我嘗試添加一個計時器到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 [email protected] 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
確切位置在哪裏我,我想補充的計時器?
字符串需要用'!字符串#等號進行比較()''不!='。另外,'定時器'在哪裏實例化? – 2013-03-17 17:32:00
說明:請確保發現不爲空,檢查,如果使用(!「是找到」 .equals(發現)) – 2013-03-17 17:32:39
我刪除找到的字符串只是爲了安全起見,但同樣的錯誤。我更新了線程並添加了計時器代碼。 – Ammar 2013-03-17 17:39:12