android - bindService() reported errors with NullPointerException -
i trying bind service , activity can setup service time , present on activty. program reported errors as:
public void starttimeservice(view v){ startservice(intent); log.d(tag,"--starttimeservice--"); bindservice(intent, con, service.bind_auto_create); log.d(tag,"post--starttimeservice")**; tv.settext("current time is:/n"+mbinder.getservice().gettime());
running program can log as: --starttimeservice-- , post--starttimeservice,but report error still:
07-13 08:23:19.832: e/androidruntime(3217): caused by: java.lang.nullpointerexception: attempt invoke virtual method 'com.himanmin.xu.servicedemo.timeservice com.himanmin.xu.servicedemo.timeservice$mybinder.getservice()' on null object reference
the code timeservice.java following:
public class timeservice extends service { private final string tag = "timeservice"; private int state; private int count; private boolean quit; public class mybinder extends binder{ public timeservice getservice(){ log.d(tag,"--getservice()"); return timeservice.this; } } private mybinder mbinder = new mybinder(); public int getstate(){ //get state of service log.d(tag,"--getstate()"); return state; } public string gettime(){ log.d(tag,"--gettime()"); simpledateformat dateformat24 = new simpledateformat("yyyy-mm-dd hh:mm:ss"); //12 format //simpledateformat dateformat12 = new simpledateformat("yyyy-mm-dd hh:mm:ss"); return dateformat24.format(calendar.getinstance().gettime()); } public int getcount(){ log.d(tag,"--getcount()"); return count; } @override public ibinder onbind(intent intent) { // todo auto-generated method stub log.d(tag,"--onbind"); system.out.println("service binded"); return mbinder; } @override public void oncreate(){ super.oncreate(); log.d(tag,"--oncreate");** system.out.println("service time created"); count = (int) system.currenttimemillis(); } @override public boolean onunbind(intent intent){ log.d(tag,"--onunbind"); system.out.println("serveice unbinded"); count = (int) (system.currenttimemillis() - count); return true; } @override public void ondestroy(){ super.ondestroy(); log.d(tag,"--ondestroy"); this.quit = true; system.out.println("service time destoryed"); count = (int) (system.currenttimemillis() - count); } }
after running bindservice()
,method oncreate()
of timeservice
class invoked,it should logcat --oncreate,but there no such log, taht means bindservice(intent, con, service.bind_auto_create)
has not ran. can not figure out reason that. intent , mainfest.xml follows:
private final string serviceaction = "com.himanmin.xu.servicedemo.time_service"; intent = new intent(); intent.setaction(serviceaction); <service android:name = "com.himanmin.xu.servicedemo.timeservice"> <intent-filter> <action android:name="com.himanmin.xu.servicedemo.time_service"/> </intent-filter> </service>
can guys me figure problem out?
the problem seems service takes time start. when call:
bindservice(intent, con, service.bind_auto_create);
the method fire intent , return immediately. android system start service @ soonest available time. therefore when call:
tv.settext("current time is:/n"+mbinder.getservice().gettime());
your service hasn't started yet, why null pointer exception.
to solve this, must implement service connection described in android bound services doc page (modified fit case):
/** defines callbacks service binding, passed bindservice() */ private serviceconnection mconnection = new serviceconnection() { @override public void onserviceconnected(componentname classname, ibinder service) { // we've bound localservice, cast ibinder , localservice instance mbinder = (mybinder) service; tv.settext("current time is:/n"+mbinder.getservice().gettime()); mbound = true; } @override public void onservicedisconnected(componentname arg0) { mbound = false; } };
only after onserviceconnected method called , mbound set true can use mbinder object.
Comments
Post a Comment