134

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)
JJD
  • 50,076
  • 60
  • 203
  • 339
michael
  • 106,540
  • 116
  • 246
  • 346
  • 8
    How are we supposed to know without posting some code? – Falmarri Oct 28 '11 at 18:19
  • 1
    Possible duplicate of [Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"](http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window) – blahdiblah Jan 11 '13 at 21:59
  • 1
    Please refer this answer , the main thing to post link of this answer is there is nice conversation in comments over this answer which describe why this problem coming and what is the best way to deal with it [http://stackoverflow.com/a/7229248/501483](http://stackoverflow.com/a/7229248/501483) – dharmendra Jul 08 '13 at 09:30
  • If you use Service, read http://stackoverflow.com/questions/23516689/unable-to-add-window-token-null-is-not-for-an-application-from-service. – CoolMind May 05 '16 at 13:52

12 Answers12

422

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);
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 1
    Thanks. But I did not use new Dialog(getApplicationContext()); I only use ' new AlertDialog.Builder(mContext);' where mContext is a referent to an activity. – michael Nov 03 '11 at 21:36
  • 10
    Then mContext should be MyActivity.this. – Luis Mar 12 '12 at 22:09
  • 10
    The android docs (http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog) for creating a custom dialog have this error. Context mContext = getApplicationContext(); Dialog dialog = new Dialog(mContext); Your fix leads to Dialog dialog = new Dialog(this); Which works! Thanks – bnieland Sep 21 '12 at 00:19
  • 1
    http://developer.android.com/guide/topics/ui/dialogs.html has been fixed (since a while). Just pointing this out for reference. The pattern has been improved as well. :) – Martin Marconcini May 03 '13 at 17:49
  • Always Create Every Dialog with passing `YourActivity.this` – Pratik Butani Jan 03 '14 at 06:34
  • @ツFellinLovewithAndroidツ what to do if i want a dialog over fragment – Sagar Devanga Dec 23 '14 at 10:11
  • 2
    Use `getActivity()` @SagarDevanga – Pratik Butani Dec 23 '14 at 10:13
  • Thank you @ツFellinLovewithAndroidツ that worked but i didnt understand why are we passing activity when it requires context – Sagar Devanga Dec 23 '14 at 10:46
  • @SagarDevanga, because an Activity is also a Context. It is a subclass of a context. – gbenroscience Jan 19 '18 at 11:08
34

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" />
codezjx
  • 9,012
  • 5
  • 47
  • 57
13

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 ;)

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
postNuKe
  • 341
  • 3
  • 8
  • 1
    Your answer is correct, however it will only work when you are in the activity scope. If you are in an inner scope, you have to use `MyActivity.class` instead of `this` because `this` refers to a different object. – Shahar Sep 15 '15 at 12:27
  • 1
    Also, `this` will make sure your `Dialog` inherits your theme. – TheRealChx101 Nov 10 '15 at 03:46
12

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

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Priyank Joshi
  • 151
  • 1
  • 4
  • Why use getParent()?, what's mean? – José Castro Feb 27 '13 at 07:27
  • 2
    When you are showing any dialog in another class that does not extend the Activity class so you may not get the context of that class that is extending the Activity. So to get the context inside your class where you are trying to show the dialog but not extending the Activity you can use the getParent() which returns you the context of that superior activity. – Priyank Joshi Mar 04 '13 at 05:53
  • 1
    Thanks @PriyankJoshi , you saved my day, it also solves the weird exception of "Android Unable to add window is not valid; is your activity running?". – Eslam Yousef Mohammed Dec 24 '13 at 14:50
  • How could i use the getParent() ? – vinidog Jul 10 '16 at 06:53
  • Thank you! It worked in my case. I was calling `this.parent.getActivity().getApplicationContext()` but `this.parent.getActivity()` did the trick. – ace_ventura Feb 15 '19 at 21:46
3

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.

Harshid
  • 5,701
  • 4
  • 37
  • 50
2

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{...
Matias
  • 21
  • 1
2

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
Kunchok Tashi
  • 2,413
  • 1
  • 22
  • 30
1

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

Anonymous-E
  • 827
  • 11
  • 29
1

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

Ganesh Garad
  • 381
  • 2
  • 6
0

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.

Soorya
  • 179
  • 1
  • 5
0

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

Makvin
  • 3,475
  • 27
  • 26
0

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());
Rajan
  • 292
  • 2
  • 10