Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );
Findviewbyid (R. Id. button1). setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
New alertdialog. Builder (getapplicationcontext ())
. Settitle (R. String. dialogtitle)
. Setmessage (R. String. dialogmessage)
. Setpositivebutton ("OK", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
Toast. maketext (getapplicationcontext (), "click OK. ", Toast. length_short). Show ();
}
}). Show ();
}
});
}
Error: unable to add window token null is not for an application
Cause of exception: In the alertdialog creation statement public alertdialog. Builder (context), the context obtained by getapplicationcontext () cannot be used, but the activity must be used.
Solution:
Change New alertdialog. Builder (getapplicationcontext () to new alertdialog. Builder (mainactivity. This)
Mainactivity is the name of the current activity.