BadTokenException: Unable to add window
<p>I have an issue where I receive a <code>BadTokenException</code> when the screen times out, and then the user returns to my application. I have a button which opens up a <code>ContextMenu</code> when clicked. </p>

<p>It works fine normally, but when the user lets the screen time out, and then returns to the application, a <code>BadTokenException</code> is thrown.</p>

<pre><code>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)
</code></pre>

<p>If I catch the Exception, the button never opens up the contextmenu in this case, but other controls on the page continue to work. </p>

<p>If I minimize the application with the home screen, and then return, the application crashes with the following trace:</p>

<pre><code>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)
</code></pre>

<p>Here's how I'm adding the ContextMenu:</p>

<pre><code>b.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {
                if (contextMenuOpen) return;// popup options
                contextMenuOpen = view.showContextMenu();

            }
</code></pre>

<p>I do not call registerForContextMenu with the button, but rather the main view itself. Is this incorrect?</p>
