Fix null pointer exception when updating action bar shadow.

Check for valid activity when trying to access the action bar as the
activity can become null when in monkey test.

Change-Id: I684d873b9eabb9d8461e99bb4385d411a48c0c52
Fix: 64084651
Test: make RunSettingsRoboTests
This commit is contained in:
Doris Ling
2017-07-27 11:14:44 -07:00
parent d6ab7dee09
commit d53a3128e8
2 changed files with 12 additions and 1 deletions

View File

@@ -121,7 +121,7 @@ public class ActionBarShadowController implements LifecycleObserver, OnStart, On
final boolean shouldShowShadow = view.canScrollVertically(-1); final boolean shouldShowShadow = view.canScrollVertically(-1);
if (mAnchorView != null) { if (mAnchorView != null) {
mAnchorView.setElevation(shouldShowShadow ? ELEVATION_HIGH : ELEVATION_LOW); mAnchorView.setElevation(shouldShowShadow ? ELEVATION_HIGH : ELEVATION_LOW);
} else { } else if (mActivity != null) { // activity can become null when running monkey
final ActionBar actionBar = mActivity.getActionBar(); final ActionBar actionBar = mActivity.getActionBar();
if (actionBar != null) { if (actionBar != null) {
actionBar.setElevation(shouldShowShadow ? ELEVATION_HIGH : ELEVATION_LOW); actionBar.setElevation(shouldShowShadow ? ELEVATION_HIGH : ELEVATION_LOW);

View File

@@ -104,4 +104,15 @@ public class ActionBarShadowControllerTest {
verify(mRecyclerView, times(2)).addOnScrollListener(any()); verify(mRecyclerView, times(2)).addOnScrollListener(any());
} }
@Test
public void onScrolled_nullAnchorViewAndActivity_shouldNotCrash() {
final Activity activity = null;
final ActionBarShadowController controller =
ActionBarShadowController.attachToRecyclerView(activity, mLifecycle, mRecyclerView);
// Scroll
controller.mScrollChangeWatcher.onScrolled(mRecyclerView, 10 /* dx */, 10 /* dy */);
// no crash
}
} }