我试图绑定服务和活动,以便设置一个服务来获得时间并将其显示在活动中。但该程序报告的错误如下:
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());运行程序可以获得日志为:--startTimeService-和,但仍然报告错误:
07-13 08:23:19.832: E/AndroidRuntime(3217):由: java.lang.NullPointerException:试图在空对象引用上调用虚拟方法'com.himanmin.xu.servicedemo.TimeService com.himanmin.xu.servicedemo.TimeService$MyBinder.getService()‘引起的
代码TimeService.java如下:
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 the 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 is Binded");
return mBinder;
}
@Override
public void onCreate(){
super.onCreate();
Log.d(tag,"--onCreate");**
System.out.println("Service for time is created");
count = (int) System.currentTimeMillis();
}
@Override
public boolean onUnbind(Intent intent){
Log.d(tag,"--onUnbind");
System.out.println("Serveice is 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 for time is destoryed");
count = (int) (System.currentTimeMillis() - count);
}
}运行bindService()后,将调用TimeService类的方法onCreate(),它应该是logcat,但是没有这样的日志,taht意味着bindService(intent, con, Service.BIND_AUTO_CREATE)没有运行。我不知道为什么会这样。意图和mainfest.xml如下:
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>你们能帮我解决这个问题吗?
发布于 2015-07-14 07:11:32
问题似乎在于服务的启动需要时间。当你打电话:
bindService(intent, con, Service.BIND_AUTO_CREATE);该方法将触发意图,并立即返回。然后Android系统将在最快的可用时间启动您的服务。因此,当你打电话:
tv.setText("current time is:/n"+mBinder.getService().getTime());您的服务尚未启动,这就是获得空指针异常的原因。
要解决这个问题,您必须实现Android绑定服务文档页面中描述的服务连接(为了适合您的情况稍微修改一下):
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
mBinder = (MyBinder) service;
tv.setText("current time is:/n"+mBinder.getService().getTime());
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};只有在调用onServiceConnected方法并将mBound设置为true之后,才能使用mBinder对象。
https://stackoverflow.com/questions/31399623
复制相似问题