WindowManager $ BadTokenException:无法在弹出窗口中添加window-token

时间:2013-06-06 10:17:03

标签: android dialog popupwindow

我根据following link这里的对话框制作了自定义弹出对话框。 enter image description here

我有按钮列表来显示弹出窗口。单击这些按钮时,将显示此对话框。我的问题有点奇怪。有时当我在对话框中选择笔记编辑文本时,我得到了WindowManager $ BadTokenException。

06-06 12:34:57.878: E/AndroidRuntime(22951): FATAL EXCEPTION: main
06-06 12:34:57.878: E/AndroidRuntime(22951): android.view.WindowManager$BadTokenException: Unable to add window --
token android.view.ViewRootImpl$W@417d8d98 is not valid; is your activity running?
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$PinnedPopupWindow.updatePosition(Editor.java:2147)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$PinnedPopupWindow.show(Editor.java:2104)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:2349)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor.showSuggestions(Editor.java:1647)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.widget.Editor$1.run(Editor.java:1546)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.os.Handler.handleCallback(Handler.java:615)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.os.Looper.loop(Looper.java:137)
06-06 12:34:57.878: E/AndroidRuntime(22951): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-06 12:34:57.878: E/AndroidRuntime(22951): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 12:34:57.878: E/AndroidRuntime(22951): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 12:34:57.878: E/AndroidRuntime(22951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-06 12:34:57.878: E/AndroidRuntime(22951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-06 12:34:57.878: E/AndroidRuntime(22951): at dalvik.system.NativeStart.main(Native Method)

我不知道为什么?我没有改变编码。我在mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)构造函数中添加了popupwindow以显示键盘上方的对话框。如果我在模拟器上运行,没有问题。在真实的设备中,我得到了例外。有什么想法吗?

在按钮监听器中,我实现了这样的

 if(dialogNote == null) {
  dialogNote = new QuickActionNote(AddNewNoteActivity.this);
 }
 dialogNote.show(btn);

感谢。

2 个答案:

答案 0 :(得分:2)

此问题是由于手机键盘中启用了自动提示,并且您尝试单击具有预填充数据的编辑文本。使用android:inputType =“textNoSuggestions”来解决你的问题。

答案 1 :(得分:1)

我遇到了完全相同的问题。似乎PopupWindow有一些问题来填充软键盘。扼杀的是,我只能在Nexus10上重现它。现在还不确定原因。