Follow the custom dialog section in the android manual to learn how to use the custom dialog. the following error occurs:
android.view.WindowManager$BadTokenException:
Unableto add window--tokennullisnotforan application
The Code logic on the manual is as follows:
Layoutinflater Inflater = (layoutinflater) getapplicationcontext (). getsystemservice (layout_inflater_service );
View layout = Inflater. Inflate (R. layout. custom_dialog,
(Viewgroup) findviewbyid (R. Id. layout_root ));
Textview TV = (textview) layout. findviewbyid (R. Id. custom_text );
TV. settext ("Hello, this is a custom dialfo ");
Imageview IV = (imageview) layout. findviewbyid (R. Id. custom_image );
Iv. setimageresource (R. drawable. Icon );
Builder = new alertdialog. Builder (getapplicationcontext ());
Builder. setview (layout );
Dialog = builder. Create ();
Change the red mark statementBuilder = new alertdialog. Builder (this );
Error cause:
You need to use your activty as the context for the dialog not the application.
Android is looking for a view to display the dialog in and the application does not have one but youractivity will. the application is an object which ties your app together and has a context as it knows about which locale you're re running in and so on,
It relies on activities to display things to the user.