java - Launching Dialog from non-activity -


i have dialog works, until added code try , launch dialog , crashes dialogfragment.class error. popup.show error occurs.

public class processlocation extends dialogfragment {

context context; sharedpreferences domaintoname; sharedpreferences nametodomain; sharedpreferences defaults; popupmessage popup; int locationtally;  // constructor processlocation(context context) {     this.context = context; }    public void processlocation(string domain, string name, string password) {          domaintoname = context.getsharedpreferences("domaintoname",  context.mode_private); //domain name = friendly name         nametodomain = context.getsharedpreferences("nametodomain",  context.mode_private); //friendly name = domain name/password/acm         defaults = context.getsharedpreferences("defaults",  context.mode_private); //friendly name = domain name/password/acm         string checkname = nametodomain.getstring(name,null);          locationtally = defaults.getint("tally",0);         final editor domainedit = domaintoname.edit();         final editor nameedit = nametodomain.edit();         //popup = new popupmessage();          // check see if location friendly name exists already, if entry overwritten new data         if (checkname == null) {              locationtally++;              updatepreferences(locationtally, name, domain, password);         //   popup.message = "location has been added";         //   popup.show(getfragmentmanager(), "popupmsgfragment");             }         else {             nameedit.remove(name);             nameedit.commit();             domainedit.remove(domain);             domainedit.remove(domain+"pw");             domainedit.commit();             updatepreferences(locationtally, name, domain, password);         }      }   public void updatepreferences(int tally, string name, string domain, string password) {       popup = new popupmessage();       domaintoname = context.getsharedpreferences("domaintoname",  context.mode_private);    //domain name = friendly name      nametodomain = context.getsharedpreferences("nametodomain",  context.mode_private); //friendly name = domain name/password/acm      defaults = context.getsharedpreferences("defaults",  context.mode_private); //friendly name = domain name/password/acm      final editor domainedit = domaintoname.edit();      final editor nameedit = nametodomain.edit();      final editor defaultedit = defaults.edit();       string defaultlocation = defaults.getstring("defaultlocation", "none set");      if(defaultlocation.equals("")) {          defaultedit.putstring("defaultlocation",name);      }       nameedit.putstring(name,domain);      nameedit.commit();      defaultedit.putint("tally",tally);      defaultedit.commit();      domainedit.putstring(domain,name);      domainedit.putstring(domain+"pw",password);      domainedit.commit();       system.out.println("mycoutput:- tally: " + locationtally);      system.out.println("mycoutput:- domain name: " + nametodomain.getstring(name,""));      system.out.println("mycoutput:- name domain: " + domaintoname.getstring(domain,""));      system.out.println("mycoutput:- password: " + domaintoname.getstring(domain+"pw",""));       popup.popuptype = "info";      popup.message = name + " has been added successfully";      popup.show(getactivity().getfragmentmanager(), "popupmsgfragment");   } 

}

the class containing popup is:

public class popupmessage extends dialogfragment {   string message = ""; addlocation addlocation; string popuptype = "";  context mcontext;  public popupmessage() {     mcontext = getactivity(); } // constructor   @override public dialog oncreatedialog(bundle savedinstancestate) {      addlocation = new addlocation();      // use builder class convenient dialog construction     alertdialog.builder builder = new alertdialog.builder(getactivity());      if (popuptype.equals("addloc")) {         builder.setmessage(message)            .setpositivebutton("add location", new dialoginterface.onclicklistener() {                public void onclick(dialoginterface dialog, int id) {                    if (popuptype.equals("addloc")) {                    addlocation.show(getfragmentmanager(), "popupmsgfragment");                    }                }            })            .setnegativebutton("ok", new dialoginterface.onclicklistener() {         public void onclick(dialoginterface dialog, int id) {           //             };            });     }     if (popuptype.equals("info")) {         builder.setmessage(message)         .setpositivebutton("ok", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int id) {              }         });      }      // create alertdialog object , return     return builder.create(); } 

}

thanks

error meessage (all throws nullpointer):

enter image description here

you did not provide addlocation class. assuming different class processlocation.

in popupmessage class, try adding:

context mcontext;  public popupmessage() {     mcontext = getactivity(); } 

and in processlocation class, modify follows:

replace popup.show(getfragmentmanager(), "popupmsgfragment");

by popup.show(getactivity().getfragmentmanager(), "popupmsgfragment");


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -