From 018e3d97d0d5b704c7fbaf6477c35e9b9d93b29d Mon Sep 17 00:00:00 2001 From: Joanne Chung Date: Wed, 18 Aug 2021 10:32:34 +0800 Subject: [PATCH] Bug Fix: fix NullPointerException while scrolling message apps The issue will occur when the view is removed from the hierarchy by the time the translation response returns. Do a null check to make sure the view still exists. Bug: 196933332 Test: atest CtsTranslationTestCases Tese: manual. not see the crash when scrolling the apps. Change-Id: Ifee933a802385750d431800ef50f6f2c306ccc0f --- .../view/translation/UiTranslationController.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/translation/UiTranslationController.java b/core/java/android/view/translation/UiTranslationController.java index a8335918cc843..442d099f06785 100644 --- a/core/java/android/view/translation/UiTranslationController.java +++ b/core/java/android/view/translation/UiTranslationController.java @@ -356,7 +356,11 @@ public class UiTranslationController { } for (int i = 0; i < translatedResult.size(); i++) { final AutofillId autofillId = new AutofillId(translatedResult.keyAt(i)); - final View view = mViews.get(autofillId).get(); + final WeakReference viewRef = mViews.get(autofillId); + if (viewRef == null) { + continue; + } + final View view = viewRef.get(); if (view == null) { Log.w(TAG, "onTranslationCompleted: the view for autofill id " + autofillId + " may be gone."); @@ -416,7 +420,11 @@ public class UiTranslationController { Log.w(TAG, "No AutofillId is set in ViewTranslationResponse"); continue; } - final View view = mViews.get(autofillId).get(); + final WeakReference viewRef = mViews.get(autofillId); + if (viewRef == null) { + continue; + } + final View view = viewRef.get(); if (view == null) { Log.w(TAG, "onTranslationCompleted: the view for autofill id " + autofillId + " may be gone.");