我试图绑定服务和活动,以便我可以设置服务来获取时间并将其呈现在活动上。但该计划报告的错误为:
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--并post-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()
后,将调用onCreate()
类的方法TimeService
,它应该是logcat --onCreate,但是没有这样的日志,taht意味着
bindService(intent, con, Service.BIND_AUTO_CREATE)
没有跑。我无法弄清楚原因。
intent和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>
你能帮助我解决问题吗?
答案 0 :(得分:0)
问题似乎是服务需要时间才能启动。当你打电话:
bindService(intent, con, Service.BIND_AUTO_CREATE);
该方法将触发一个intent并立即返回。然后Android系统将在最短的可用时间启动您的服务。因此,当您致电:
tv.setText("current time is:/n"+mBinder.getService().getTime());
您的服务尚未启动,这就是您获得空指针异常的原因。
要解决此问题,您必须按照Android Bound Services doc page中的说明实现服务连接(稍微修改以适合您的情况):
/** 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对象。