java - Android Dialog not appearing -


i picked android sdk , eclipse , decided write simple dialog pops when click button, found out showdialog() method has been deprecated , dialogfragment best way go making one, here is:

package net.learn2develop.dialog;  import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.content.dialoginterface; import android.os.bundle; import android.support.v4.app.dialogfragment; import android.support.v4.app.fragmentmanager; import android.view.view; import android.widget.button;  public class dialogwindow extends activity{  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.dialog_window);      button btn = (button) findviewbyid(r.id.btn_dialog);     btn.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {              class mydialog extends dialogfragment {                  fragmentmanager fm = getfragmentmanager();                  public mydialog() {                  }                  @override                 public dialog oncreatedialog(bundle savedinstancestate) {                     //using builder class                     alertdialog.builder builder = new alertdialog.builder(getactivity());                     builder.setmessage("simple dialog")                         .setpositivebutton("nothing", new dialoginterface.onclicklistener() {                              @override                             public void onclick(dialoginterface dialog, int which) {                                 //toast.maketext(getbasecontext(), "ok then", toast.length_short).show();                             }                         })                         .setnegativebutton("cancel", new dialoginterface.onclicklistener() {                              @override                             public void onclick(dialoginterface dialog, int which) {                                 //toast.maketext(getbasecontext(), "cancel", toast.length_short).show();                              }                         });                      return builder.create();                 }               }              mydialog md = new mydialog();             md.show(md.fm, "dialog");         }     }); } } 

the debugger shows error at:

md.show(md.fm, "dialog"); 

on further inspection, fm variable null according variables tab in debugger, why , there more efficient solution this? new android, sorry if easy question.

you should use alertdialog in case, not dialogfragment. here how it:

public class dialogwindow extends activity{  @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.dialog_window);  button btn = (button) findviewbyid(r.id.btn_dialog); btn.setonclicklistener(new view.onclicklistener() {      @override     public void onclick(view v) {         createdialog.show();     } }); }  public dialog createdialog() {       //using builder class       alertdialog.builder builder = new alertdialog.builder(getactivity());       builder.setmessage("simple dialog")          .setpositivebutton("nothing", new dialoginterface.onclicklistener() {               @override              public void onclick(dialoginterface dialog, int which) {                  //getbasecontext() not advised (i can't remember why right know read it. may want read more method getactivity work toast.                  //toast.maketext(getactivity(), "ok then", toast.length_short).show();           }           })          .setnegativebutton("cancel", new dialoginterface.onclicklistener() {               @override              public void onclick(dialoginterface dialog, int which) {                //toast.maketext(getactivity(), "cancel", toast.length_short).show();             }             });              return builder.create();      } } 

dialogfragments should used in conjunction other fragments, in case of single activity best use alertdialog.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -