"Android.View.Windowmanager$Badtokenexception: Unable to Add Window" on Buider.Show()

android.view.WindowManager$BadTokenException: Unable to add window on buider.show()

android.view.WindowManager$BadTokenException: Unable to add window"

Problem :

This exception occurs when the app is trying to notify the user from
the background thread (AsyncTask) by opening a Dialog.

If you are trying to modify the UI from background thread (usually
from onPostExecute() of AsyncTask) and if the activity enters
finishing stage i.e.) explicitly calling finish(), user pressing home
or back button or activity clean up made by Android then you get this
error.

Reason :

The reason for this exception is that, as the exception message says,
the activity has finished but you are trying to display a dialog with
a context of the finished activity. Since there is no window for the
dialog to display the android runtime throws this exception.

Solution:

Use isFinishing() method which is called by Android to check whether
this activity is in the process of finishing: be it explicit finish()
call or activity clean up made by Android. By using this method it is
very easy to avoid opening dialog from background thread when activity
is finishing.

Also maintain a weak reference for the activity (and not a strong
reference so that activity can be destroyed once not needed) and check
if the activity is not finishing before performing any UI using this
activity reference (i.e. showing a dialog).

eg.

private class chkSubscription extends AsyncTask<String, Void, String>{

private final WeakReference<login> loginActivityWeakRef;

public chkSubscription (login loginActivity) {
super();
this.loginActivityWeakRef= new WeakReference<login >(loginActivity)
}

protected String doInBackground(String... params) {
//web service call
}

protected void onPostExecute(String result) {
if(page.contains("error")) //when not subscribed
{
if (loginActivityWeakRef.get() != null && !loginActivityWeakRef.get().isFinishing()) {
AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
builder.setCancelable(true);
builder.setMessage(sucObject);
builder.setInverseBackgroundForced(true);

builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
dialog.dismiss();
}
});

builder.show();
}
}
}
}

Update :

Window Tokens:

As its name implies, a window token is a special type of Binder token
that the window manager uses to uniquely identify a window in the
system. Window tokens are important for security because they make it
impossible for malicious applications to draw on top of the windows of
other applications. The window manager protects against this by
requiring applications to pass their application's window token as
part of each request to add or remove a window. If the tokens don't
match, the window manager rejects the request and throws a
BadTokenException. Without window tokens, this necessary
identification step wouldn't be possible and the window manager
wouldn't be able to protect itself from malicious applications.

 A real-world scenario:

When an application starts up for the first time,
the ActivityManagerService creates a special kind of window token
called an application window token, which uniquely identifies the
application's top-level container window. The activity manager gives
this token to both the application and the window manager, and the
application sends the token to the window manager each time it wants
to add a new window to the screen. This ensures secure interaction
between the application and the window manager (by making it
impossible to add windows on top of other applications), and also
makes it easy for the activity manager to make direct requests to the
window manager.

android.view.WindowManager$BadTokenException: Unable to add window while showing a AlertDialog from broadcast receiver

You can only display an AlertDialog from an Activity, not a BroadcastReceiver.

You can start an activity from a BroadcastReceiver, and so you can create an activity with a dialog theme (e.g., Theme.Material.Dialog), so it is styled like a dialog.

Note, though, that showing a dialog when the device starts up is rather user-hostile, not to mention impractical (e.g., the device has a lockscreen). Consider showing a Notification instead.

Java android android.view.WindowManager$BadTokenException: Unable to add window

Add Current ActivityName.this instead of this.

 AlertDialog.Builder dialog = new AlertDialog.Builder(YourCurrentActivityName.this);

Android 1.6: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Instead of :
Context appContext = this.getApplicationContext();
you should use a pointer to the activity you're in (probably this).

I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from developer.android.com :(

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@739b5ae is not valid

Wherever you are adding builder.show(), please put the condition whether the activity is finished or not.

Change your context reference to Activity in UpdateClass Constructor:

public UpdateClass(Activity context) {
//this.context also should be Activity, so please make it as a Activity Reference
this.context = context;

db = new DAO(context);
db.open();

lastAuthor = db.getLastAuthor();
lastQuote = db.getLastQuote();

siteUrl = context.getResources().getString(R.string.siteUrl);
updatesUrl = siteUrl + "site/get_updates/" + String.valueOf(lastAuthor)
+ "/" + String.valueOf(lastQuote);

auPictureDir = siteUrl + "global/uploads/levels/";

}

Like:

if(!context.isFinishing()){ //here activity means your activity class

builder.show();
}


Related Topics



Leave a reply



Submit