1

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?

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • 2
    instance of `activity` is no longer valid ... fx.: you calling some function from `ActivityX` instance and var `activity` is set to instance of `ActivityA` (or already closed instance `ActivityX`) – Selvin Apr 11 '13 at 10:58
  • @Selvin can you give me an example solution.. – Darko Petkovski Apr 11 '13 at 10:59
  • it's hard to explain ... but you can tried to pass Activity in method call instead using instance stored in 'activity' var ... fx. : if you have method like `void static DoSomething(String param1)` use `void static DoSomething(Activity activity, String param1)` instead – Selvin Apr 11 '13 at 11:04

3 Answers3

8

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Umesh
  • 1,609
  • 1
  • 17
  • 28
1

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
0

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

fantaxy025025
  • 809
  • 8
  • 7