Fix bug 3200615 - "MenuPopupHelper cannot be used without an anchor"
Add protection against views disappearing before previously posted
Runnables attempt to show a menu anchored to them.
Change-Id: Ia2a322e76665e61feb5bdb92377d5066cb6d5b04
diff --git a/core/java/com/android/internal/view/menu/ActionMenuView.java b/core/java/com/android/internal/view/menu/ActionMenuView.java
index 2c5baba..9381675 100644
--- a/core/java/com/android/internal/view/menu/ActionMenuView.java
+++ b/core/java/com/android/internal/view/menu/ActionMenuView.java
@@ -66,9 +66,10 @@
}
public void run() {
- mOverflowPopup = mPopup;
- mPopup.show();
- mPostedOpenRunnable = null;
+ if (mPopup.tryShow()) {
+ mOverflowPopup = mPopup;
+ mPostedOpenRunnable = null;
+ }
}
}
diff --git a/core/java/com/android/internal/view/menu/MenuPopupHelper.java b/core/java/com/android/internal/view/menu/MenuPopupHelper.java
index a070835..fe41f52 100644
--- a/core/java/com/android/internal/view/menu/MenuPopupHelper.java
+++ b/core/java/com/android/internal/view/menu/MenuPopupHelper.java
@@ -69,6 +69,12 @@
}
public void show() {
+ if (!tryShow()) {
+ throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
+ }
+ }
+
+ public boolean tryShow() {
mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle);
mPopup.setOnItemClickListener(this);
mPopup.setOnDismissListener(this);
@@ -92,13 +98,14 @@
mTreeObserver.addOnGlobalLayoutListener(this);
mPopup.setAnchorView(anchor);
} else {
- throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
+ return false;
}
mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopup.show();
mPopup.getListView().setOnKeyListener(this);
+ return true;
}
public void dismiss() {