3

I have publishing an app in Android market. I have tested in HTC and Samsung, it is working fine. But today I got an error in my App stated below.

android.view.WindowManager$BadTokenException
Source method 
ViewRoot.setView()

In Stack trace,

android.view.WindowManager$BadTokenException: Unable to add window -- token
android.os.BinderProxy@4059be38 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:532)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:200)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at android.app.AlertDialog$Builder.show(AlertDialog.java:810)
at com.newtglobal.android.Trace.serverBusy(Trace.java:1041)
at com.newtglobal.android.Trace.access$12(Trace.java:1013)
at com.newtglobal.android.Trace$13$1.run(Trace.java:975)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

For applications that include obfuscated code, stack traces contain obfuscated symbol names. If the application was obfuscated using ProGuard, you can unobfuscate the stack trace using the "retrace" tool, included in the Android SDK Tools r8 and later. For more information, see the ProGuard document.

What is the Problem. How to get rid of this error??? But I have tested in HTC and Samsung, it is working fine.

Asraf
  • 333
  • 1
  • 10
  • 24

3 Answers3

2

I'm having a very similar issue with an app of mine; from some research, it looks like you want to make sure that you're using the Activity Context when creating your AlertDialog (not the Application Context), and also want to make sure you're using the topmost parent Activity (see more on the accepted answer for this post: https://stackoverflow.com/a/4011090/202870).

Community
  • 1
  • 1
areyling
  • 2,181
  • 2
  • 20
  • 25
0
@Override
public void onCreate(Bundle savedInstanceState) {
    // do all your other stuff here

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mProgressDialog = ProgressDialog.show(
               MyActvity.this.getApplicationContext(), "",
               MyActvity.this.getString(R.string.loading), true);

            // start time consuming background process here
        }
    }, 1000); // starting it in 1 second
}
peter
  • 1,028
  • 9
  • 21
0

"BadTokenException Unable to add window" can occur quite a few times in Android.

Many views such as 'popup window' or 'dialog box' needs a parent view/activity to launch. If, in any case it happens that your parent view has still not yet been initialized, but a view dependent on it is called, BadTokenException is thrown.

To avoid this first you need to make sure that the parent view is already attached to the phone window. For activity it can be done using 'onActivityAttached' or 'onAttachedToWindow()' etc. Then call your child view and it should work fine.

If the activity or the parent view is already attached use it's own conext and NOT getAppliationContext().

If its in Activity class itself you can pass the same object (this/ CLASS_NAME.this).

For your case I think when setView is called, its not getting the parent view. Or else pass the activity's context.

How this will help/

Happy Coding. :)