- Private Context Mcontext;
- @Override
- protected void onCreate (Bundle savedinstancestate) {mcontext = Getapplicationcontext ();
- System.out.println ("mcontext=" + mcontext);
- }
- New Alertdialog.builder (Mcontext)
- . SetIcon (Android. R.drawable.ic_dialog_alert)
- . Settitle ("warnning")
- . Setmessage (
- "You forget to write the message. Do you want to fill out it?? ")
- . Setpositivebutton ("Yes", Positivelistener). Setnegativebutton (
- "No", Negativelistener). Create (). Show ();
This error is caused by the new Alertdialog.builder (Mcontext), although the parameter here is Alertdialog.builder (context context) However, we cannot use the context obtained by Getapplicationcontext () and must use activity because there is only one activity to add a form.
Workaround:
You can create a dialog correctly by populating the parameters in the new Alertdialog.builder (context context) with activity.this (the activity is the name of your activity).
- New Alertdialog.builder (myactivity. This)
- . SetIcon (Android. R.drawable.ic_dialog_alert)
- . Settitle ("warnning")
- . Setmessage (
- "You forget to write the message. Do you want to fill out it?? ")
- . Setpositivebutton ("Yes", Positivelistener). Setnegativebutton (
- "No", Negativelistener). Create (). Show ();
Android for use-----windowmanager$badtokenexception:unable to add window--token null was not for an application