2011-07-01 98 views
10

我遇到問題,當屏幕超時,然後用戶返回到我的應用程序時,我收到BadTokenException。點擊時我有一個按鈕,打開ContextMenuBadTokenException:無法添加窗口

它正常工作正常,但是當用戶讓屏幕超時,然後返回到應用程序時,引發BadTokenException

07-01 14:46:42.763: WARN/WindowManager(1105): Attempted to add window with token that is a sub-window: [email protected] 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 [email protected] 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,而是使用主視圖本身。這是不正確的?

回答

13

您正在堅持對已經銷燬的Context的引用(顯式或通過創建對話框或Toast或其他相關項目)(通常是因爲您使用的是onCreateDialog或者您將Activity傳遞給了某些當活動被銷燬時沒有被銷燬的其他進程)。

+0

感謝您的回覆。你介意看我的編輯原始帖子,並確保它是有效的?我在onClick函數中接收對視圖的有效引用。 –

+0

這實在不足以說明你在做什麼錯誤:真的需要查看更多的代碼來確定具體細節。你可能會嘗試的是將'setOnClickListener'調用移動到Activity的'onResume'回調中(確保你在調用'onResume'時調用超類方法)並查看是否仍然存在問題。 – Femi

+0

我最終通過不同的途徑解決了這個問題,這可能不適用於其他任何人,並且沒有真正解決問題,所以我不會在這裏解釋它。謝謝 –

0

當我調用方法showContextMenu()只是在onResume我抓到了同樣的異常。我使用Handler解決了這個問題

@Override 
protected void onResume() { 
    super.onResume(); 

    if (contextMenuOpened) { 
     new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       choosePicBtn.showContextMenu(); 
      } 
     }); 
    } 
} 
相關問題