Android 'Unable to add window -- token null is not for an application' exception

AndroidAndroid DialogRuntimeexceptionAndroid Windowmanager

Android Problem Overview


I get the following Android exception when I try to open a dialog. Can someone please help me understand what is going on and how can I fix this problem?

android.view.WindowManager$BadTokenException: 
  Unable to add window -- token null is not for an application
	at android.view.ViewRoot.setView(ViewRoot.java:509)
	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
	at android.app.Dialog.show(Dialog.java:241)

Android Solutions


Solution 1 - Android

I'm guessing - are you trying to create Dialog with an application context? Something like this:

new Dialog(getApplicationContext());

This is wrong. You need to use an Activity context.

You have to try like:

new Dialog(YourActivity.this);

Solution 2 - Android

You can continue to use getApplicationContext(), but before use, you should add this flag: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT), and the error will not show.

And don't forget to add permission:

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

Solution 3 - Android

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

new Dialog(getApplicationContext());

So I had to change for:

new Dialog(this);

And it works fine for me ;)

Solution 4 - Android

Try getParent() at the argument place of context like new AlertDialog.Builder(getParent()); Hope it will work, it worked for me.

Solution 5 - Android

I'm guessing - are you trying to create Dialog using.

 getApplicationContext()
 mContext which is passed by activity.

if You displaying dialog non activity class then you have to pass activity as a parameter.

Activity activity=YourActivity.this;

Now it will be work great.

If you find any trouble then let me know.

Solution 6 - Android

I tried with this in the context field:

this.getActivity().getParent()

and it works fine for me. This was from a class which extends from "Fragment":

public class filtro extends Fragment{...

Solution 7 - Android

Hello there if you are using adapter there might be a chance.
All you need to know when you used any dialog in adapter,getContext(),context or activity won't work sometime.

Here is the trick I used v.getRootView().getContext() where v is the view object you are referencing.
Eg.


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(v.getRootView().getContext(), date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });  
If you are getting this problem because of alert dialog.
Refer [here][1] But it is same concept.


  [1]: https://stackoverflow.com/questions/6367771/displaying-alertdialog-inside-a-custom-listadapter-class

Solution 8 - Android

I got the same exception. what i do to fix this is to pass instance of the dialog as parameter into function and use it instead of pass only context then using getContext(). this solution solve my problem, hope it can help

Solution 9 - Android

I got this exception, when I tried to open Progress Dialog under Cordova Plugin by using below two cases,

  1. new ProgressDialog(this.cordova.getActivity().getParent());

  2. new ProgressDialog(this.cordova.getActivity().getApplicationContext());

Later changed like this,

new ProgressDialog(this.cordova.getActivity());

Its working fine for me.

Solution 10 - Android

Use this and context not worked for me..but MyActivityName.this worked. Hope this helps anyone who need it.

Solution 11 - Android

I solved this error by add below user-permission in AndroidManifest.xml

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

Also, Initialize Alert dialog with Activity Name:

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

For More Details, visit==> How to create Alert Dialog in Android

Solution 12 - Android

Just in case you are trying to achive showing the Dialog from a Fragment. Just use "getActivity()" method.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmichaelView Question on Stackoverflow
Solution 1 - AndroidPeter KnegoView Answer on Stackoverflow
Solution 2 - AndroidcodezjxView Answer on Stackoverflow
Solution 3 - AndroidpostNuKeView Answer on Stackoverflow
Solution 4 - AndroidPriyank JoshiView Answer on Stackoverflow
Solution 5 - AndroidHarshidView Answer on Stackoverflow
Solution 6 - AndroidMatiasView Answer on Stackoverflow
Solution 7 - AndroidcodingwithtashiView Answer on Stackoverflow
Solution 8 - AndroidAnonymous-EView Answer on Stackoverflow
Solution 9 - AndroidSooryaView Answer on Stackoverflow
Solution 10 - AndroidMakvinView Answer on Stackoverflow
Solution 11 - AndroidGanesh GaradView Answer on Stackoverflow
Solution 12 - AndroidRajanView Answer on Stackoverflow