android - DialogFragment callback to Target Fragment using interface -


i trying send data dialogfragment targeted fragment not working.

i have written following code, through exception:

java.lang.nullpointerexception: attempt invoke interface method 'void com.x.x.fragmentalertdialog$communicator.seti(java.lang.string)' on null object reference 

code:

public class fragmentalertdialog extends dialogfragment {  communicator callback;  public interface communicator {     void seti(string name); }  public static fragmentalertdialog newinstance(string id, int title, int items) {     fragmentalertdialog frag = new fragmentalertdialog();     bundle args = new bundle();     args.putstring("id", id);     args.putint("title", title);     args.putint("items", items);     frag.setarguments(args);     return frag; }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     callback = (communicator) gettargetfragment(); }  @override public dialog oncreatedialog(bundle savedinstancestate) {     string id = getarguments().getstring("id");     int title = getarguments().getint("title");     string[] items = getresources().getstringarray(getarguments().getint("items"));     switch (id) {         case mainactivity.dialog_1:             return new alertdialog.builder(getactivity())                     .settitle(title)                     .setitems(items, new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int which) {                             callback.seti("important data");                         }                     }).create();     }     return null; } 

fragment class

public class fragmentxyxy extends fragment implements view.onclicklistener, fragmentalertdialog.communicator {  @override public void onclick(view v) {     switch (v.getid()) {         case r.id.et_i:             dialogfragment newfragment = fragmentalertdialog.newinstance(mainactivity.dialog_i, r.string.i_select, r.array.i_array);             newfragment.settargetfragment(this, 0);             newfragment.show(getfragmentmanager(), "dialog");     } }  @override public void seti(string name) {     texti.settext(name); } 

can please help. thank you

code modified- newfragment.settargetfragment(this, 0);

you have call settargetfragment

settargetfragment(this, 0); 

in order reference fragmentxyxy, in dialogfragment.

@override public void onclick(view v) {     switch (v.getid()) {         case r.id.et_i:             dialogfragment newfragment = fragmentalertdialog.newinstance(mainactivity.dialog_i, r.string.i_select, r.array.i_array);             newfragment.settargetfragment(this, 0);             newfragment.show(getfragmentmanager(), "dialog");     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -