Android对话框导致android.view.WindowManager $ BadTokenException:无法添加窗口

时间:2013-04-11 10:46:25

标签: android multithreading android-dialog

我已将我的活动放在childview中,现在我无法显示来自我的活动和适配器的对话框。在我的logCat中我得到了

04-11 12:39:59.823: E/AndroidRuntime(12831): FATAL EXCEPTION: main
04-11 12:39:59.823: E/AndroidRuntime(12831): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@41971f18 is not valid; is your activity running?
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:513)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.Window$LocalWindowManager.addView(Window.java:537)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.Dialog.show(Dialog.java:278)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.myapp.functions.DownloadsDetailsAdapter$1$2.run(DownloadsDetailsAdapter.java:148)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.Activity.runOnUiThread(Activity.java:4170)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.myapp.functions.DownloadsDetailsAdapter$1.onClick(DownloadsDetailsAdapter.java:139)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.View.performClick(View.java:3511)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.View$PerformClick.run(View.java:14105)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.os.Handler.handleCallback(Handler.java:605)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.os.Looper.loop(Looper.java:137)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.ActivityThread.main(ActivityThread.java:4440)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at dalvik.system.NativeStart.main(Native Method)

以下是我试图显示对话框的示例。

AlertDialog.Builder builder = new AlertDialog.Builder(
                            activity);
                    builder.setMessage(R.string.are_you_sure)
                            .setPositiveButton(R.string.yes,
                                    dialogClickListener)
                            .setNegativeButton(R.string.no, dialogClickListener)
                            .show();

注意:此代码在我进行更改之前正在运行。我应该在新的runnable中运行它,但如果我必须这样做,有人可以告诉我我该怎么做?

3 个答案:

答案 0 :(得分:8)

我也遇到了同样的问题。我使用了标签栏。只需使用getParent()代替youractivity.this

我希望这会有所帮助。

答案 1 :(得分:1)

activity对象必须是当前显示在屏幕上的活动。 如果它已经暂停或者没有构建显示它将会出现此错误。

答案 2 :(得分:0)

在这里看到ActivityGroup的答案。

对于ActivityGroup的子活动,当我们在子活动(如PrompDialog)中使用Context时,我们无法确定它是否始终存在和存在。 例如:

mPromptDialog = new LoginPromptDialog(this);//this is used as Context
//But when we use this context it may be destroyed.
//So this is the parent instance
mPromptDialog = new LoginPromptDialog(this.getParent());

偏好: http://www.cnblogs.com/kaima/archive/2011/08/04/2127813.html