PopupWindow $ BadTokenException:无法添加窗口 – 标记null无效

显示PopupWindow时出现以下错误。 这个错误是由这个行触发的:

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

mapView是一个MapView,没有什么是空的。 堆栈跟踪:

 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) 

这是我的活动(扩展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); } 

谢谢你分享你的想法

你太早显示你的popup窗口。 你可以在Onresume上发布showatlocation的延迟运行,试试看

编辑:这篇文章似乎有相同的问题回答在Android活动中创build一个popup窗口的问题

同样的问题发生在我身上,当我尝试在活动中显示popup菜单时,我也得到了同样的excpetion,但我遇到问题ñ通过提供上下文ActivityName.this而不是getApplicationContext()而解决,是的,它为我工作可能会帮助别人

发生此exception时有两种情况。 一个是nandeesh提到的。 其他情况在这里提到: http : //blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/

确保你处理他们两个

一个popup的父母本身不能是一个popup窗口。 他们的父母都必须是一样的。 所以,如果在popup窗口中创build一个popup窗口,则必须保存父窗口的popup窗口并将其设置为父窗口。

这里是一个例子

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

popupWindowFocusChanged()上的popupwindow.show()方法是正确的。

尝试显示如下的stream行音乐

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

尝试使用它

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