当屏幕超时,然后用户返回我的应用程序时,我会收到一个BadTokenException
的问题。我有一个按钮,当点击它时会打开一个ContextMenu
。
通常情况下它可以正常工作,但当用户让屏幕超时,然后返回应用程序时,将抛出BadTokenException
异常。
07-01 14:46:42.763: WARN/WindowManager(1105): Attempted to add window with token that is a sub-window: android.os.BinderProxy@44af17c8. Aborting.
07-01 14:46:42.771: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.View.showContextMenu(View.java:2444)
07-01 14:46:42.771: WARN/System.err(1725): at com.??.??.ui.cards.ViewActivity$3.onClick(ViewActivity.java:353)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.View.performClick(View.java:2408)
07-01 14:46:42.771: WARN/System.err(1725): at android.view.View$PerformClick.run(View.java:8816)
07-01 14:46:42.771: WARN/System.err(1725): at android.os.Handler.handleCallback(Handler.java:587)
07-01 14:46:42.771: WARN/System.err(1725): at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 14:46:42.771: WARN/System.err(1725): at android.os.Looper.loop(Looper.java:123)
07-01 14:46:42.771: WARN/System.err(1725): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 14:46:42.771: WARN/System.err(1725): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 14:46:42.771: WARN/System.err(1725): at java.lang.reflect.Method.invoke(Method.java:521)
07-01 14:46:42.771: WARN/System.err(1725): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-01 14:46:42.771: WARN/System.err(1725): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-01 14:46:42.771: WARN/System.err(1725): at dalvik.system.NativeStart.main(Native Method)
07-01 14:46:43.005: WARN/System.err(1725): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@44757528 is not valid; is your activity running?
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewRoot.setView(ViewRoot.java:505)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-01 14:46:43.005: WARN/System.err(1725): at android.app.Dialog.show(Dialog.java:241)
07-01 14:46:43.005: WARN/System.err(1725): at com.android.internal.view.menu.MenuDialogHelper.show(MenuDialogHelper.java:86)
07-01 14:46:43.005: WARN/System.err(1725): at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:88)
07-01 14:46:43.005: WARN/System.err(1725): at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:1860)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
07-01 14:46:43.005: WARN/System.err(1725): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:459)
如果我捕获了异常,那么在这种情况下该按钮就不会打开上下文菜单,但页面上的其他控件仍然可以工作。
如果我将应用程序最小化并返回主屏幕,应用程序将崩溃,并显示以下跟踪信息:
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): FATAL EXCEPTION: main
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): java.lang.NullPointerException
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.view.WindowManagerImpl.removeViewLocked(WindowManagerImpl.java:239)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.view.WindowManagerImpl.closeAll(WindowManagerImpl.java:293)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3687)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.app.ActivityThread.access$2900(ActivityThread.java:125)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.os.Looper.loop(Looper.java:123)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at java.lang.reflect.Method.invoke(Method.java:521)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-01 15:10:50.439: ERROR/AndroidRuntime(1931): at dalvik.system.NativeStart.main(Native Method)
以下是我如何添加ContextMenu的代码:
b.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View view ) {
if (contextMenuOpen) return;// popup options
contextMenuOpen = view.showContextMenu();
}
我不是用按钮调用registerForContextMenu,而是用主视图本身。这样做是否不正确?
setOnClickListener
调用移动到 Activity 的onResume
回调中(在重写onResume
时确保同时调用超类方法),看看是否还有问题。 - FemiEditText nameField;
,然后在onCreate
中调用EditText nameField= (EditText) findViewById(R.id.nameField);
。您应该在onPause()
中销毁引用,并在onResume()
中重新创建。至少这对我解决了问题。 - Xeos