java - Passing a variable value between activities -


i trying pass number edittext box in activity1 activity2 when button pressed, want number appear in toast or dialog box when action bar button pressed in activity2. have set intent , coded think should work seems crashing activity2 everytime. put in line of code should fetch variable. able see i'm going wrong. know passing data within variable should easy task.
appreciated.

activity1:  public class activity1 extends activity {   @override protected void oncreate(bundle savedinstancestate) {  super.oncreate(savedinstancestate); //this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.screen_settings); this.setrequestedorientation(activityinfo.screen_orientation_portrait);  final edittext inputtxt1 = (edittext) findviewbyid(r.id.conphonenum);     button savebtn1 = (button) findviewbyid(r.id.btnsave1);     savebtn1.setonclicklistener(new view.onclicklistener() {        @override        public void onclick(view view) {           string phonenum1 = inputtxt1.gettext().tostring();           savenum1(phonenum1);    intent passintent = new intent();   passintent.putextra("phonenum", phonenum1);      }     }); }   public void savenum1(string phonenum1) {  alertdialog.builder dlgalert  = new alertdialog.builder(this);  dlgalert.setmessage("saved " + phonenum1); dlgalert.setpositivebutton("ok", null); dlgalert.setcancelable(true);  dlgalert.setpositivebutton("ok",     new dialoginterface.onclicklistener() {     public void onclick(dialoginterface dialog, int which) {           //dismiss dialog           }     });     dlgalert.create().show(); } } 

activity2:

public class activity2 extends activity {        //this line stops activity running string conphonenum = getintent().getextras().getstring("phonenum");   @suppresslint("cutpasteid") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main);  actionbar actionbar = getactionbar(); actionbar.hide(); actionbar.show(); actionbar.setcustomview(r.layout.actionbar_custom_view_home); actionbar.setdisplayshowcustomenabled(true); actionbar.setdisplayshowhomeenabled(false);  }   public boolean oncreateoptionsmenu1(menu menu) {     // inflate menu; adds items action bar if present.     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.main, menu);     return true; }      @override  public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {     case r.id.menu_load:      toast toast= toast.maketext(this, conphonenumber, toast.length_short);       toast.setgravity(gravity.center, 0, -100);           toast.show();       break;           default:              break;              }     return true;     }   } 

logcat information:

07-31 15:51:50.135: e/androidruntime(779): fatal exception: main 07-31 15:51:50.135: e/androidruntime(779): java.lang.runtimeexception: unable start activity     componentinfo{com.cam.data /com.cam.data.activity2}: java.lang.nullpointerexception  07-31 15:51:50.135: e/androidruntime(779):     @     android.app.activitythread.performlaunchactivity(activitythread.java:2110)  07-31 15:51:50.135: e/androidruntime(779):     @     android.app.activitythread.handlelaunchactivity     (activitythread.java:2135)  07-31 15:51:50.135: e/androidruntime(779):     @ android.app.activitythread.access$700    (activitythread.java:140)  07-31 15:51:50.135: e/androidruntime(779):     @ android.app.activitythread$h.handlemessage     (activitythread.java:1237)  07-31 15:51:50.135: e/androidruntime(779):     @ android.os.handler.dispatchmessage     (handler.java:99) 07-31 15:51:50.135: e/androidruntime(779):  @ android.os.looper.loop(looper.java:137)  07-31 15:51:50.135: e/androidruntime(779):     @ android.app.activitythread.main    (activitythread.java:4921)  07-31 15:51:50.135: e/androidruntime(779):     @ java.lang.reflect.method.invokenative(native method) 07-31 15:51:50.135: e/androidruntime(779):  @ java.lang.reflect.method.invoke(method.java:511)  07-31 15:51:50.135: e/androidruntime(779):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 07-31 15:51:50.135: e/androidruntime(779):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 07-31 15:51:50.135: e/androidruntime(779):  @ dalvik.system.nativestart.main(native method) 07-31 15:51:50.135: e/androidruntime(779): caused by: java.lang.nullpointerexception 07-31 15:51:50.135: e/androidruntime(779):  @ com.cam.data.activity2.oncreate(activity2.java:66)  07-31 15:51:50.135: e/androidruntime(779):     @ android.app.activity.performcreate(activity.java:5206) 07-31 15:51:50.135: e/androidruntime(779):  @     android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) 07-31 15:51:50.135: e/androidruntime(779):  @      android.app.activitythread.performlaunchactivity(activitythread.java:2074) 

update 1: conphonenum = getintent().getextras().getstring("phonenum"); still crashing activity. have found if remove .getextras() not crash not display data. don't know if kind of indicator why making app crash? maybe can help

update 2:
update on this, ended scrapping , using global variables defined in global class can used activity @ time. seems better way of doing kind of thing unless of course absolutely need pass variable.
if work out why wouldn't work, incase other people come across problem

replace this

  intent passintent = new intent();   passintent.putextra("phonenum", phonenum1);   

by

  intent passintent = new intent(activity1.this,activity2.class);   passintent.putextra("phonenum", phonenum1);   startactivity(passintent); 

to in activity2 :

declare conphonenum class member

  string conphonenum ; 

in oncreate

  conphnonenum = getintent().getextras().getstring("phonenum"); 

then

  toast toast= toast.maketext(this, conphonenumber, toast.length_short); 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -