Darko Petkovski
Darko Petkovski

Reputation: 3912

Android dialogs are causing android.view.WindowManager$BadTokenException: Unable to add window

I have putted my activities in childview and now I cannot display dialogs from my activities and adapters. In my logCat i'm getting

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)

And here is an example of how I'm trying to display dialogs.

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();

Note: this code was working before I make the change. I thing I should run this in new runnable but if should I have to do that, can anybody tell me how can I do that?

Upvotes: 1

Views: 6773

Answers (3)

Umesh
Umesh

Reputation: 1609

I also faced the same problem. I have used tab bar for this. Just use getParent() instead of youractivity.this.

I hope this will help.

Upvotes: 8

fantaxy025025
fantaxy025025

Reputation: 809

see here the answer for ActivityGroup.

For child activity of a ActivityGroup, we cannot be sure that it always exist and live when we use the Context in the child activity such as PrompDialog. For example:

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());

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

Upvotes: 0

Budius
Budius

Reputation: 39846

the activity object must be the activity that is currently shown on the screen. If it's something that was already paused or not build show yet it will give this error.

Upvotes: 1

Related Questions