我尝试将计时器添加到 OnCreate 方法、Onstart 甚至 AsyncTask,但程序会崩溃。不太清楚为什么!这是我的代码的一部分,在这种情况下,我添加到 OnStart。
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
private void startTimer()
{
if (found != "Yes Found")
{
TimerTask updateProfile = new CustomTimerTask(MyService.this);
timer.scheduleAtFixedRate(updateProfile, 0, 60*1000);
}
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
//spool.release();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
startTimer();
}
定时器代码
public class CustomTimerTask extends TimerTask {
private Context context;
private Handler mHandler = new Handler();
// Write Custom Constructor to pass Context
public CustomTimerTask(Context con) {
this.context = con;
}
@Override
public void run() {
// TODO Auto-generated method stub
//some code here
}
}
错误信息
03-17 13:16:35.661: E/AndroidRuntime(7634): FATAL EXCEPTION: main
03-17 13:16:35.661: E/AndroidRuntime(7634): java.lang.RuntimeException: Unable to start service com.example.saber.MyService@415c0510 with Intent { cmp=xxxx }: java.lang.NullPointerException
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2387)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.app.ActivityThread.access$1900(ActivityThread.java:127)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.os.Looper.loop(Looper.java:137)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.app.ActivityThread.main(ActivityThread.java:4511)
03-17 13:16:35.661: E/AndroidRuntime(7634): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 13:16:35.661: E/AndroidRuntime(7634): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 13:16:35.661: E/AndroidRuntime(7634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
03-17 13:16:35.661: E/AndroidRuntime(7634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
03-17 13:16:35.661: E/AndroidRuntime(7634): at dalvik.system.NativeStart.main(Native Method)
03-17 13:16:35.661: E/AndroidRuntime(7634): Caused by: java.lang.NullPointerException
03-17 13:16:35.661: E/AndroidRuntime(7634): at com.example.saber.MyService.startTimer(MyService.java:67)
03-17 13:16:35.661: E/AndroidRuntime(7634): at com.example.saber.MyService.onStart(MyService.java:86)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.app.Service.onStartCommand(Service.java:438)
03-17 13:16:35.661: E/AndroidRuntime(7634): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2370)
03-17 13:16:35.661: E/AndroidRuntime(7634): ... 10 more
我到底应该在哪里添加计时器?