显示PopupWindow时出现以下错误.错误由行触发:
checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0);
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
这是我活动的代码(扩展了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);
}
Run Code Online (Sandbox Code Playgroud)
感谢您分享您的想法
And*_* me 89
当我尝试在活动中显示弹出菜单时也发生了同样的问题我也得到了同样的激励,但我通过提供上下文遇到问题解决
YourActivityName.this 而不是getApplicationContext()在
Dialog dialog = new Dialog(getApplicationContext());
是的,它对我有用,可能会帮助别人
nan*_*esh 71
你过早地展示你的弹出窗口.你可以在Onresume上发布一个延迟的runnable for showatlocation,试一试
编辑: 这篇文章似乎有同样的问题解答在Android Activity中创建一个弹出窗口的问题
小智 28
使用:
YourActivityName.this
Run Code Online (Sandbox Code Playgroud)
代替:
getApplicationContext();
Run Code Online (Sandbox Code Playgroud)
The*_*Man 10
发生此异常时有两种情况.nandeesh提到了一个.这里提到了其他方案:http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/
确保你同时处理它们
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
正确的方法是 onWindowFocusChanged() 处的 popupwindow.show()。