From a591d7a08262bf29336cc60bd3981e804e063ba2 Mon Sep 17 00:00:00 2001 From: Yi-Ling Chuang Date: Wed, 11 Mar 2020 16:52:44 +0800 Subject: [PATCH] Fix NPE when coming back to settings homepage. When users navigate back to the homepage, if there is a slice being displayed, slice callback will be triggered. It will then check if this slice has any error hint, where NPE would occur if the slice turns out to be null. Hence, add a null check before checking slice hint. Fixes: 148158780 Test: rebuild and navigate back to the homepage. Change-Id: I2b04dc0d64a223cc85f6af55b2ae2dbc5f3e1f40 --- .../contextualcards/slices/SliceContextualCardRenderer.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java b/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java index 05531d6d5de..5f1e9559c5c 100644 --- a/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java +++ b/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java @@ -120,6 +120,11 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life swipeBackground.setVisibility(View.GONE); } sliceLiveData.observe(mLifecycleOwner, slice -> { + if (slice == null) { + // The logic handling this case is in OnErrorListener. Adding this check is to + // prevent from NPE when it calls .hasHint(). + return; + } if (slice.hasHint(HINT_ERROR)) { Log.w(TAG, "Slice has HINT_ERROR, skipping rendering. uri=" + slice.getUri()); mSliceLiveDataMap.get(slice.getUri()).removeObservers(mLifecycleOwner);