Thursday, September 2, 2010

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Hi guys,
if you are facing this problem then it is problem with your context which you are passing in the AlertDialog, so make sure you are passing the Context which is initialized by
Context context = this;

And some times in case of Service, you can face this problem while you are using the correct Context. In that case use the following code before show() method.

AlertDialog ad;
// code for creating. .create();
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
ad.show();

It will work.

3 comments:

Nabeel said...

I have spent lot of time to solve this issue.It really helped me.
Very helpful sharing..thanks

Unknown said...

@Nabeel ...thanks for appreciation

Emi said...

Thanks, thanks!!