我使用的是android。最近我編碼了一個重發數據。 但發生非法狀態異常定時器被取消。我該如何處理Timer被取消錯誤?
這是我的logcat:
java.lang.RuntimeException: Unable to start receiver kr.co.iosystem.blackeyeonandroid.util.NetworkChangeReceiver: java.lang.IllegalStateException: Timer was canceled
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2414)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Timer was canceled
at java.util.Timer.scheduleImpl(Timer.java:561)
at java.util.Timer.schedule(Timer.java:459)
at kr.co.iosystem.blackeyeonandroid.sender.DataSender.sendPicData(DataSender.java:251)
at kr.co.iosystem.blackeyeonandroid.sender.DataSender.restart(DataSender.java:425)
at kr.co.iosystem.blackeyeonandroid.sender.DataSender.update(DataSender.java:337)
at java.util.Observable.notifyObservers(Observable.java:138)
at kr.co.iosystem.blackeyeonandroid.util.AutoObservable.notifyObservers(AutoObservable.java:13)
at kr.co.iosystem.blackeyeonandroid.main.Agency.notifyMessage(Agency.java:88)
at kr.co.iosystem.blackeyeonandroid.util.NetworkChangeReceiver.onReceive(NetworkChangeReceiver.java:200)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
我覺得這就是例外是發生
private Timer mTimer = null; //global variable
@Override
public void run() {
mTimer = new Timer();
...
}
@Override
public void update(){
if (this.senderState == SenderState.STOPPED) {
this.mTimer.cancel();
this.restart(); //Timer exception ...
}
我能知道的部分我要去的地方錯了嗎?
您無法重新啓動取消的定時器。你需要創建一個新的。 –
@GabeSechan你的建議不要用取消,重啓的方法相同? –
您可以在DataSender對象上使用重新啓動。但是你必須創建一個新的Timer對象,而不僅僅是啓動一個新的Timer對象。 –