Java Android应用程序的自动注销
我知道这个问题有几个答案,但它对我不起作用,目前我的代码是:Java Android应用程序的自动注销,java,android,Java,Android,我知道这个问题有几个答案,但它对我不起作用,目前我的代码是: public class LogoutService extends Service { public static CountDownTimer timer; @Override public void onCreate(){ // TODO Auto-generated method stub super.onCreate(); timer = new Co
public class LogoutService extends Service {
public static CountDownTimer timer;
@Override
public void onCreate(){
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1 * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v(TAG, "Service Started");
}
public void onFinish() {
Log.v(TAG, "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
在我的每项活动中:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LogoutService.timer.start();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
LogoutService.timer.cancel();
}
但是,当我尝试启动包含此代码的任何活动时,我得到一个“无法恢复活动java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.os.CountDownTimer android.os.CountDownTimer.start()”。我的应用程序并不太复杂,它完全处于脱机状态,因此没有web服务调用或任何事情,我只需要在x分钟后返回登录活动。有人有什么想法吗
这是我使用的一个:并且您没有启动服务,因此您没有分配计时器。实际上,您不应该为此使用static。您没有启动服务,因此没有分配计时器。确实,您不应该为此使用static。首先启动您的服务我如何启动服务以及在哪里?哦,好吧,我按照您的链接,在我的onCreate中使用了下面的代码,但我仍然得到了错误。知道为什么吗?startService(新意图(这个,LogoutService.class));首先启动您的服务我如何启动服务以及在哪里?哦,好吧,我按照您的链接,在我的onCreate中使用了下面的代码,但我仍然得到了错误。知道为什么吗?startService(新意图(这个,LogoutService.class));我在onCreate中启动了该服务,但仍然收到相同的错误。我在onCreate中启动了该服务,但仍然收到相同的错误。