Android 1.6: "Android.View.Windowmanager$Badtokenexception: Unable to Add Window - Token Null Is Not For an Application"

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 :Unable to add window -- token null is not for an application

You should use Activity.this instead of getApplicationContext()

$BadTokenException: Unable to add window -- token null is not for an application - Kotlin

It causes error, because it can't find the current view's context to show the AlertDialog

So what you need to do is replacing val context = applicationContext to

val context = view.context

Android Error: Unable to add window -- token null is not for an application

To add AlertDialog to your Activity or Fragment you have to use your Activity's instance, not your application's which you are doing in your code. Create your AlertDialog like this :

AlertDialog.Builder alertDialog  = new AlertDialog.Builder(getActivity());

And that should do the trick for you! : )



Related Topics



Leave a reply



Submit