Android对话框导致android.view.WindowManager $BadTokenException:无法添加窗口
作者:互联网
我已将活动置于子视图中,现在无法显示活动和适配器中的对话框.在我的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中运行它,但是如果我必须这样做,谁能告诉我我该怎么做?
解决方法:
我也面临同样的问题.我已经使用标签栏了.只需使用getParent()而不是youractivity.this.
我希望这将有所帮助.
标签:android,multithreading,android-dialog 来源: https://codeday.me/bug/20191009/1878907.html