OSDN Git Service

Add a null check for getWindow()
authorSiarhei Vishniakou <svv@google.com>
Tue, 5 Mar 2019 18:05:01 +0000 (10:05 -0800)
committerSiarhei Vishniakou <svv@google.com>
Wed, 6 Mar 2019 19:07:22 +0000 (11:07 -0800)
Protect against crash surfaced by monkey, where it seems that the window
of the dialog is no longer present. If not present, don't need to post
dismiss runnable.

Bug: 124327419
Test: adb shell monkey -p com.google.android.GoogleCamera -s 999 -v 20000
Change-Id: Ic03b8501bb73725e04544fcb2397532be531988e

core/java/android/preference/DialogPreference.java

index 4b5a7b4..a615f2d 100644 (file)
@@ -342,16 +342,31 @@ public abstract class DialogPreference extends Preference implements
         dialog.show();
     }
 
+    /**
+     * Get the DecorView.
+     * @return the DecorView for the current dialog window, if it exists.
+     * If the window does not exist, null is returned.
+     */
+    private View getDecorView() {
+        if (mDialog != null && mDialog.getWindow() != null) {
+            return mDialog.getWindow().getDecorView();
+        }
+        return null;
+    }
+
     void postDismiss() {
         removeDismissCallbacks();
-        View decorView = mDialog.getWindow().getDecorView();
-        decorView.post(mDismissRunnable);
+        View decorView = getDecorView();
+        if (decorView != null) {
+            // If decorView is null, dialog was already dismissed
+            decorView.post(mDismissRunnable);
+        }
     }
 
     private void removeDismissCallbacks() {
-        if (mDialog != null && mDialog.getWindow() != null
-                && mDialog.getWindow().getDecorView() != null) {
-            mDialog.getWindow().getDecorView().removeCallbacks(mDismissRunnable);
+        View decorView = getDecorView();
+        if (decorView != null) {
+            decorView.removeCallbacks(mDismissRunnable);
         }
     }