android - Interface NullPointerException - Sending Listener w/ Data to MainActivity on Button Click within Fragment -


been working on sending string data mainactivity on button click within fragment. doesn't seem difficult procedure, i'm getting tied somewhere , receiving nullpointerexception. i'm assuming has way i'm using interface. new android, suggestions , comments appreciated. thanks!

interface code:

public interface btninterface {     public void btnclicked(string x, string y, string z, string w); } 

main activity snippet:

public class mainactivity extends activity implements btninterface{      fragment_x frag;     fragmenttransaction ft;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          frag = new fragment_x();         frag.setinterface(this);         ft = getfragmentmanager().begintransaction();         ft.add(r.id._frag_container, frag);         ft.commit();     }      @override     public void btnclicked(string x, string y,string z, string w) {         log.d("data-check", x);      } 

fragment snippet:

public class fragment_x extends fragment {      view v;     button btn;     btninterface buttoninterface;     string x, y, z, w;     string  choices1 = {"random", "values"}     string  choices2 = {"random", "values"}     string  choices3 = {"random", "values"}     string  choices4 = {"random", "values"}      public void setinterface(btninterface buttoninterface) {         this.buttoninterface = buttoninterface;     }      public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {          v = inflater.inflate(r.layout.frag_layout, container,             false);          spin1 = (spinner) v.findviewbyid(r.id.spin1);         arrayadapter<string> adapter = new arrayadapter<string>(getactivity(),             r.layout.spinner_layout, choices1);         adapter.setdropdownviewresource(r.layout.spinner_dropdown);         spinsomewhere.setadapter(adapter);          spin2 = (spinner) v.findviewbyid(r.id.spin2);         arrayadapter<string> adapter1 = new arrayadapter<string>(getactivity(),             r.layout.spinner_layout, choices2);         adapter1.setdropdownviewresource(r.layout.spinner_dropdown);         spinfor.setadapter(adapter1);          spin3 = (spinner) v.findviewbyid(r.id.spin3);         arrayadapter<string> adapter2 = new arrayadapter<string>(getactivity(),             r.layout.spinner_layout, choices4);         adapter2.setdropdownviewresource(r.layout.spinner_dropdown);         spinxp.setadapter(adapter2);          spin4 = (spinner) v.findviewbyid(r.id.spin4);         arrayadapter<string> adapter3 = new arrayadapter<string>(getactivity(),             r.layout.spinner_layout, choices4);         adapter3.setdropdownviewresource(r.layout.spinner_dropdown);         spinwith.setadapter(adapter3);          btn.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                  x = spin1.getselecteditem().tostring();                 y = spin2.getselecteditem().tostring();                 z = spin3.getselecteditem().tostring();                 w = spin4.getselecteditem().tostring();                   buttoninterface.btnclicked(x,y,z,w);             }         });          return v;     } }   

error log:

05-30 21:27:35.230: e/androidruntime(3016): fatal exception: main 05-30 21:27:35.230: e/androidruntime(3016): process: com.ex.app, pid: 3016 05-30 21:27:35.230: e/androidruntime(3016): java.lang.runtimeexception: unable start     activity componentinfo{com.ex.app/com.ex.app.mainactivity}:    java.lang.nullpointerexception 05-30 21:27:35.230: e/androidruntime(3016):     @     android.app.activitythread.performlaunchactivity(activitythread.java:2195) 05-30 21:27:35.230: e/androidruntime(3016):     @     android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.activitythread.access$800(activitythread.java:135) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.os.handler.dispatchmessage(handler.java:102) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.os.looper.loop(looper.java:136) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.activitythread.main(activitythread.java:5017) 05-30 21:27:35.230: e/androidruntime(3016):     @ java.lang.reflect.method.invokenative(native method) 05-30 21:27:35.230: e/androidruntime(3016):     @ java.lang.reflect.method.invoke(method.java:515) 05-30 21:27:35.230: e/androidruntime(3016):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-30 21:27:35.230: e/androidruntime(3016):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-30 21:27:35.230: e/androidruntime(3016):     @ dalvik.system.nativestart.main(native method) 05-30 21:27:35.230: e/androidruntime(3016): caused by: java.lang.nullpointerexception 05-30 21:27:35.230: e/androidruntime(3016):     @ com.ex.app.fragment_x.oncreateview(fragment_x.java:75) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.fragment.performcreateview(fragment.java:1700) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:890) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1062) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.backstackrecord.run(backstackrecord.java:684) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1447) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.activity.performstart(activity.java:5240) 05-30 21:27:35.230: e/androidruntime(3016):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2168) 05-30 21:27:35.230: e/androidruntime(3016):     ... 11 more 

it null because forgot set btninterface fragment_x after instantiate it.

solution:

frag = new fragment_x(); frag.setinterface(this);     ft = getfragmentmanager().begintransaction();     ft.add(r.id._frag_container, frag);     ft.commit(); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -