我遇到一个问题,当屏幕超时时,我收到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,而是使用主 View 本身。这不正确吗?

最佳答案

您要保留对已被销毁的上下文的引用(无论是显式的,还是通过创建对话框或Toast或其他依赖项的引用),(通常是因为您使用的是onCreateDialog或您将Activity传递给了其他 Activity 被销毁时不会被销毁)。

关于java - BadTokenException : Unable to add window,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6553724/

10-09 16:56