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

ID : 10037

viewed : 12

Tags : androidandroid-dialogruntimeexceptionandroid-windowmanagerandroid

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

vote vote

99

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 :(

vote vote

88

You cannot display an application window/dialog through a Context that is not an Activity. Try passing a valid activity reference

vote vote

78

Ditto on the getApplicationContext thing.

The documents on the android site says to use it, but it doesn't work...grrrrr :-P

Just do:

dialog = new Dialog(this);  

"this" is usually your Activity from which you start the dialog.

vote vote

65

Android documents suggests to use getApplicationContext();

but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...

Ex:

AlertDialog.Builder builder = new  AlertDialog.Builder(this); 

or

AlertDialog.Builder builder = new  AlertDialog.Builder((Your Activity).this); 
vote vote

52

Instead of getApplicationContext(), just use ActivityName.this

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

Related QUESTION?