PopupWindow $BadTokenException: Unable to add window ― token null is not valid

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

问题:

I have the following error when showing a PopupWindow. The errors are triggered by the line:

checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0);

mapView is a MapView and nothing is null. The stacktrace:

01-08 18:00:09.402: E/AndroidRuntime(27768): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:513) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.view.Window$LocalWindowManager.addView(Window.java:537) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.widget.PopupWindow.invokePopup(PopupWindow.java:988) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at com.geoloc.ActivityCheckIn.onCreate(ActivityCheckIn.java:50) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.app.Activity.performCreate(Activity.java:4465) 01-08 18:00:09.402: E/AndroidRuntime(27768):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

This is the code from my activity (that extends MapActivity)

    protected void onCreate(Bundle icicle) {     super.onCreate(icicle);     setContentView(R.layout.checkin);     mapView = (MapView) findViewById(R.id.mapview);      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);     checkInPopup = new PopupWindow(inflater.inflate(CHECK_IN_POPUP_LAYOUT, null, false));     checkInPopup.setOutsideTouchable(true);     checkInPopup.setHeight(100);     checkInPopup.setWidth(200);     checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0); }

Thank you for sharing your thoughts

回答1:

you are showing your popup too early. You may post a delayed runnable for showatlocation in Onresume , Give it a try

Edit: This post seems to have the same problem answered Problems creating a Popup Window in Android Activity



回答2:

Same problem happened with me when i try to show popup menu in activity i also got same excpetion but i encounter problem n resolve by providing context ActivityName.this instead of getApplicationContext() and yes it worked for me may it will help someone else



回答3:

There are two scenarios when this exception could occur. One is mentioned by nandeesh. Other scenario is mentioned here: http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/

Make sure you handle both of them



回答4:

A popup's parent can't itself be a popup. Both of their parents must be the same. So, if you create a popup inside a popup, you must save the parent's popup and make it a parent.

here's an example



回答5:

  @Override protected void onCreate(Bundle savedInstanceState) {     View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window_layout, new LinearLayout(mContext), true);     popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);     popupWindow.setContentView(view); }     @Override public void onWindowFocusChanged(boolean hasFocus) {     if (hasFocus) {         popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);     } }

the correct way is popupwindow.show() at onWindowFocusChanged().



回答6:

Try to show the pop like below

findViewById(R.id.main_layout).post(new Runnable() {         public void run() {             mPopupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);             Button close = (Button) customView.findViewById(R.id.btn_ok);             close.setOnClickListener(new View.OnClickListener() {                 @Override                 public void onClick(View v) {                     mPopupWindow.dismiss();                     doOtherStuff();                 }             });         }     });


回答7:

Try to use it

LayoutInflater inflater = (LayoutInflater).getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflate.from(YourActivity.this).inflate(R.layout.yourLayout, null);


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