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

Below are the things you can do to get it over from issue.

  1. Instead of using

      context 
    you should use
      ActivityName.this 
  2. if you are using getApplicationContext(), then use below flag before using getApplicationContext ()

      dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)  

Add the following permission to your manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

I changed:

ProgressDialog(getApplicationContext());

to:

ProgressDialog(MainActivity.this);

,


In my case I was trying to create my dialog like this:

new Dialog(getApplicationContext());

So I had to change for:

new Dialog(MyActivity.this);

And it started working.


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! : )