Can a View on a PopupWindow display a PopupMenu?

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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,

回答1:

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();     } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!