android - How to show an AlertDialog in class which extends Application -
i have class extends application. goal show alertdialog (with 1 ok button), after apllication begins , (every time user runs app, not every time user enter main activity other activities ).
the name of main activity activity1.
so here class extends application:
   public class myapp extends application {         @override         public void oncreate() {             super.oncreate();              activity1 myactivity1=new activity1();              alertdialog.builder builder = new alertdialog.builder(myactivity1);              builder.setmessage("hello").setcancelable(false).setpositivebutton("ok",                                                                   new dialoginterface.onclicklistener() {                public void onclick(dialoginterface dialog, int id) {                     //do things                }             });             alertdialog alert = builder.create();             alert.show();         }     }   this error get:
java.lang.runtimeexception: unable create application…...nullpointerexception: attempt invoke virtual method 'android.content.pm.applicationinfo android.content.context.getapplicationinfo()' on null object reference   i guess activity1 not right argument. tried "this" argument also. mean:
alertdialog.builder builder = new alertdialog.builder(this);   it didn't help.
if has idea this, i'll appreciate help. thank you!
you need have class extends application has boolean value. used store global values last long application running.
public class myapplication extends application {     public boolean hasshownalertdialog = false; // can private , use getter/setters, but, that's frowned upon. }   in android manifest, under application, need define you're using custom implementation.
<application android:name="com.example.myapplication" [...]>   in activity1, in oncreate, check global variable:
public void oncreate(bundle bundle) {     ...     if (!(myapplication)getapplication().hasshownalertdialog) {         // show alert dialog         (myapplication)getapplication().hasshownalertdialog = true;     } }      
Comments
Post a Comment