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
This commit is contained in:
Joanne Chung
2021-08-18 10:32:34 +08:00
parent 1b5d7b4c2a
commit 018e3d97d0

View File

@@ -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<View> 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<View> 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.");