java - How to fetch toggle button value in previouse activity in Android? -
i have create application service run in background system , 2 activities.i have start service in activity1 , in activity2 have put toggle button "yes" , "no". when run application service start in activity1 , when opening activity2 activity1 toggle button status automatically "on" service status using shared preferences when pressed toggle button "off" stopping service got nullpointerexception.i have tried not working .please can 1 me.here code
public class service_demo extends activity implements onclicklistener { private static final string tag = "servicesdemo"; button buttonstart, buttonstop; // boolean value; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); system.out.println("in oncreate("); startservice(new intent(this, myservice.class)); buttonstart = (button) findviewbyid(r.id.buttonstart); buttonstart.setonclicklistener(this); boolean value=getintent().getextras().getboolean("togglebtn"); log.e("boolean value","4 toggle button in service demo"+value); } public void onclick(view src) { intent i=new intent(this , toggle_activity.class); startactivity(i); } } public class toggle_activity extends activity { togglebutton tgbutton; private boolean isservice=false; private string strservice; public final string service_prefs="serviceprefs"; private static final string strmyservice = "zdf"; public static boolean status=false; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.toggle); final sharedpreferences serviceprefs=this.getsharedpreferences("service_prefs",mode_world_readable); strservice=serviceprefs.getstring(strmyservice , "myservice"); log.e("",""+strservice); final boolean mbool = serviceprefs.getboolean("myservice", true); log.e("boolean value mbool","="+mbool); boolean b = mbool; log.e("update pref", b.tostring()); tgbutton = (togglebutton)findviewbyid(r.id.togglebutton); tgbutton.setchecked(mbool); system.out.println("*****tgbutton.setchecked(mbool1);****"); final boolean mbool1 = serviceprefs.getboolean("myservice", false); log.e("boolean value mbool","="+mbool1); final boolean c = mbool1; log.e("update pref", c.tostring()); tgbutton=(togglebutton)findviewbyid(r.id.togglebutton); tgbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if(tgbutton.ischecked()) { startservice(new intent(toggle_activity.this , myservice.class)); system.out.println("service started in togglr button"); } else { stopservice(new intent(toggle_activity.this,myservice.class)); intent intent=new intent(toggle_activity.this,service_demo.class); status=false; intent.putextra("togglebtn",status); startactivity(intent); } } }); } } fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.toggle_button/com.toggle_button.service_demo}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 05-15 12:44:32.154: e/androidruntime(793): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 05-15 12:44:32.154: e/androidruntime(793): @ android.app.activitythread.access$1500(activitythread.java:117) 05-15 12:44:32.154: e/androidruntime(793): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 05-15 12:44:32.154: e/androidruntime(793): @ android.os.handler.dispatchmessage(handler.java:99) 05-15 12:44:32.154: e/androidruntime(793): @ android.os.looper.loop(looper.java:123) 05-15 12:44:32.154: e/androidruntime(793): @ android.app.activitythread.main(activitythread.java:3683) 05-15 12:44:32.154: e/androidruntime(793): @ java.lang.reflect.method.invokenative(native method) 05-15 12:44:32.154: e/androidruntime(793): @ java.lang.reflect.method.invoke(method.java:507) 05-15 12:44:32.154: e/androidruntime(793): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 05-15 12:44:32.154: e/androidruntime(793): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 05-15 12:44:32.154: e/androidruntime(793): @ dalvik.system.nativestart.main(native method) 05-15 12:44:32.154: e/androidruntime(793): caused by: java.lang.nullpointerexception 05-15 12:44:32.154: e/androidruntime(793): @ com.toggle_button.service_demo.oncreate(service_demo.java:43) 05-15 12:44:32.154: e/androidruntime(793): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 05-15 12:44:32.154: e/androidruntime(793): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 05-15 12:44:32.154: e/androidruntime(793): ... 11 more
first activity:
string togvalue = togglebtn.gettext().tostring(); intent = new intent(first.this, sec.class); i.putextra("togvalue", togvalue); startactivity(i);
second activity:
string togvalue = getintent().getstringextra("togvalue");
Comments
Post a Comment