Reputation: 921
In Android API11+ I'm displaying a button inside a PopupWindow. I'd like to show a PopupMenu when the button is clicked, without closing the PopupWindow. Is this possible at all? I'm instantiating and initilizing the PopupMenu, but when I call popupMenu.show() I'm getting this error (LogCat + partial call stack):
02-25 13:31:38.281: W/WindowManager(528): Attempted to add window with token that is a sub-window: android.os.BinderProxy@41316cc8. Aborting. 02-25 13:31:51.257: D/AndroidRuntime(7643): Shutting down VM 02-25 13:31:51.257: W/dalvikvm(7643): threadid=1: thread exiting with uncaught exception (group=0x40a711f8) 02-25 13:31:51.320: E/AndroidRuntime(7643): FATAL EXCEPTION: main 02-25 13:31:51.320: E/AndroidRuntime(7643): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@419c3458 is not valid; is your activity running? [rest of stack]
Is this because the PopupMenu uses another PopupWindow which does not seem te be allowed from a PopupWindow? Same code runs OK when I hook it up to a button in the Activity content view.
Thanks in advance,
Upvotes: 3
Views: 1774
Reputation: 67
You can't anchor a PopupMenu from a PopupWindow view what you can do is define or anchor from a view inside roow view or top-level view here is an example:
main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/IVoptionsMenuInvis"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_weight="25"
android:paddingBottom="6dp"
android:paddingTop="6dp">
</FrameLayout>
popupwindow.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/settingseditback"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/IVoptionsMenu"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_weight="25"
android:onClick="showSettingsPopup2"
android:paddingBottom="6dp"
android:paddingTop="6dp"
android:src="@drawable/ic_actionbar_overflow_dark" />
</LinearLayout>
and finally your MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
.
.
.
public void showSettingsPopup2(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, findViewById(R.id.IVoptionsMenuInvis));
popup.setOnMenuItemClickListener(this);
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
popup.show();
}
}
Upvotes: 1