Fix NPEs in detached views.
Bug: 3297518
Change-Id: Ife1384f3a5bebbd28a29f97dc6d519f8a0b10ba9
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index 5a5b6f4..79d6a81 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -1468,12 +1468,17 @@
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
- getKeyDispatcherState().startTracking(event, this);
+ KeyEvent.DispatcherState state = getKeyDispatcherState();
+ if (state != null) {
+ state.startTracking(event, this);
+ }
return true;
- } else if (event.getAction() == KeyEvent.ACTION_UP
- && getKeyDispatcherState().isTracking(event) && !event.isCanceled()) {
- dismiss();
- return true;
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
+ KeyEvent.DispatcherState state = getKeyDispatcherState();
+ if (state != null && state.isTracking(event) && !event.isCanceled()) {
+ dismiss();
+ return true;
+ }
}
return super.dispatchKeyEvent(event);
} else {