java - NullPointerException when binding the service in the OnCreate() method -


i want bind service activity in order read service data retrieved phone , display in textview.

please refer code below: facts() method use mservice reference should not null after calling startservicefacts method , null.

however when i'm calling tfacts.settext(onfacts) method in separate button works ok. why can't bind service in oncreate method , how can achieve in order avoid using additional button?

@override protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     setcontentview(r.layout.handsetdetailsactivity);     mconnection = new connectionclass();     tfacts = (textview) findviewbyid(r.id.thandsetdtails);     intent intent = new intent(handsetdetailsactivity.this,          servicedevicefacts.class);     startservice(intent);     startservicefacts();     tfacts.settext(facts()); }  void startservicefacts() {     intent intent = new intent(this, servicedevicefacts.class);     try {         bindservice(intent, mconnection, context.bind_auto_create);     } catch (exception ex) {         log.e("exception in service", ex.tostring());     }     log.e("mbound value", mbound + ""); }  private class connectionclass implements serviceconnection {      @override     public void onserviceconnected(componentname classname, ibinder service) {         servicebinder binder = (servicebinder) service;         mservice = binder.getservice();         mbound = true;     }      @override     public void onservicedisconnected(componentname arg0) {         mbound = false;     } }; 

log cat message error :

10-07 11:41:28.007: e/androidruntime(24964): fatal exception: main 10-07 11:41:28.007: e/androidruntime(24964): java.lang.runtimeexception: unable start activity componentinfo{com.example.testma/com.testma.activities.handsetdetailsactivity}: java.lang.nullpointerexception 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1651) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1667) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.activitythread.access$1500(activitythread.java:117) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.activitythread$h.handlemessage(activitythread.java:935) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.os.handler.dispatchmessage(handler.java:99) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.os.looper.loop(looper.java:130) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.activitythread.main(activitythread.java:3687) 10-07 11:41:28.007: e/androidruntime(24964):    @ java.lang.reflect.method.invokenative(native method) 10-07 11:41:28.007: e/androidruntime(24964):    @ java.lang.reflect.method.invoke(method.java:507) 10-07 11:41:28.007: e/androidruntime(24964):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867) 10-07 11:41:28.007: e/androidruntime(24964):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:625) 10-07 11:41:28.007: e/androidruntime(24964):    @ dalvik.system.nativestart.main(native method) 10-07 11:41:28.007: e/androidruntime(24964): caused by: java.lang.nullpointerexception 10-07 11:41:28.007: e/androidruntime(24964):    @ com.testma.activities.handsetdetailsactivity.facts(handsetdetailsactivity.java:81) 10-07 11:41:28.007: e/androidruntime(24964):    @ com.testma.activities.handsetdetailsactivity.oncreate(handsetdetailsactivity.java:36) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 10-07 11:41:28.007: e/androidruntime(24964):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1615) 10-07 11:41:28.007: e/androidruntime(24964):    ... 11 more 

binding service asynchronous operation. why cannot perform service oriented operations right after calling startservicefacts().

you have wait till onserviceconnected method called in connectionclass, confirms bind activity.

regarding button mystery! reason why works fine in button-press because service has been bind before onclick of button raised.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -