我有一个问题,当屏幕超时时,我收到一个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)
b.setOnClickListener( new OnClickListener() { @Override public void onClick( View view ) { if (contextMenuOpen) return;// popup options contextMenuOpen = view.showContextMenu(); }
我不使用按钮调用registerForContextMenu,而是主视图本身.这是不正确的吗?
解决方法
您正在继承对已经被破坏的上下文(通过创建对话框或Toast或其他依赖项)的引用(通常是因为您正在使用onCreateDialog,或者您将Activity传递给某个其他进程)当活动被破坏时,不会被破坏).