If you are creating a custom Dialog for Android, and following the Android Developers’ Creating Dialogs tutorial, then most likely you would have faced a Force Close with this exception showing up in logcat. I did too. Although I figured it out quickly, it might not be easy to find out for many, so posting it here for reference. Basically, the code given in the tutorial goes something like this:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
All looks well, but when you execute it, you will get a Force Close. The error appearing in logcat would be something like this:
Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
It isn’t apparent immediately that what is causing this error. The very first line in the code “Context mContext = getApplicationContext();” is the culprit.
Solution: Just replace “getApplicationContext()” with “this” (i.e. “Context mContext = this;” ) and it will work fine.
Explanation: As to why this is exactly an issue, I’m a bit fuzzy about it myself but this much I’m sure that the contexts that you get with getApplicationContext and this are different. On reading about this function from Android SDK help:
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
I think this would mean is that getApplicationContext returns a context which is for the application itself and not the activity, while “this” would give you the context of the activity in which you are creating the dialog. I think since it is the activity which is associated with the UI (and for whom the window has been created), using the application context would have caused the crash here.


It worked for me. Thanks!
thanks man… good work
Excellent writeup! Very clear and easy to understand.
Your post helped me a lot! Thanks for sharing this tip
This is very useful, thank you for the exp!
Your post worked for me as well. (Wish they’d fix the Creating Dialogs Dev Guide.) Thank you.
I was messing around for an hour with this one before your solution worked, many thanks from Ireland
Another word of thanks. Your explanation about using getApplicationContext vs. “this” makes sense, but it definitely isn’t obvious. I’m glad I hit google immediately when I saw the error while creating a dialog.
Very helpfuly explanation, men. Thank’s, this is the key of all our problems.
Brilliant! You’re a Lifesaver.
It really helps having stuff like this on the net. Google’s own dev site sucks. just try the mapactivity tut. geez.
Too Good!!! Fixed my issue!! Thanks a bunch…
In my case, I needed the context for an AlertDialog.Builder() within an OnItemClickListener, in which case I couldn’t just use “this”. When I attempted to use getApplicationContext() I got the BadTokenException error.
After finding the source of the error from this article, I
1 – Created a variable in the class scope: “private Context thisContext;”
2 – Defined it in the onCreate method of the activity: “thisContext = this;”
3 – And used it in my OnItemClickListener as follows: “alertDialog = new AlertDialog.Builder(thisContext).create();”
thanks man, it is working for me.
i used ActivityGroup in my apps.
then setContentView(LayoutInflater.from(getParent()).inflate(R.layout.fuel,null));
when i click on DateSetDialog button error generate like
04-11 13:44:31.560: ERROR/AndroidRuntime(22212): android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
solve it plz
thanks
Read a few of your posts and have thought they were really intresting. looking forward to seeing more from you.
Cheers, worked for me.
Good Idea ,it really works ,
Thanks ! It worked for me too!
cool man it worked for me also..thanks a lot.
You are the MAN!
Tnx
Thanks a lot!
Strange that Android/Google guys used buggy code in their tutorials.
very much useful, thanks a lot:)
Doesn’t seem to work. When I try what you say I get an error on that line saying, “type mismatch: cannot convert from ExpandableListCalender.MyExpandableListAdapter to Context”
Okay… sort of figured out the problem. Well… I do not understand completly why what I changed worked as I am very new to Android but you can disregard my last post as it now works. I guess I just have to figure out why but since I am knew to programming as a whole I am guessing it is something obvious. Anyhow, thanks.
I used “XXXActivity.this” everywhere needed the object of Context,but it occured this problem sometimes yet!
Sometimes it’s ok but sometimes it shows “Uable to add Window” exception.
I just don’t know why
Thanks mate.. my issue is fixed.
I need some help on this..
I need to read an image file from the sdcard and create another image(same content with different resolution) image on the sdcard or the res folder.
I tried using FileInputStream to read the image contents, but once i use…..
fileIn = openFileInput(Environment.getExternalStorageDirectory().toString()+”/ab.jpg”);
its throwin exception.
Can anyone suggest me on this….
Do anyone have a piece of code that can read and create image on sdcard.
thank u.
Thanks its work for me…
Thanx buddy..It worked
It works, thank you very much
Much thx .. worked .. saved me lot of work
[...] 这是解释http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html。 [...]