scheduleAtFixedRate-timer does not work (Android in Eclipse)



我在Eclipse中使用scheduleatfixerate -timer为我的Android应用程序,但它不起作用:

Timer t = new Timer();
t.scheduleAtFixedRate(task, (long) 0, (long) interval*1000);

,其中interval可以为0.5。但是,这是可行的:

    Timer t = new Timer();
    t.scheduleAtFixedRate(task, (long) 0, (long) 500);

和一个时间表定时器也工作:

    Timer t = new Timer();
    t.schedule(task, (long) interval*1000);

我认为这是奇怪的,不知道为什么模拟器给出一个非法的参数异常。有人能帮我一下吗?

编辑:stack trace

03-20 12:29:54.080: E/AndroidRuntime(855): FATAL EXCEPTION: main
03-20 12:29:54.080: E/AndroidRuntime(855): Process: com.example.bulerias, PID: 855
03-20 12:29:54.080: E/AndroidRuntime(855): java.lang.IllegalArgumentException
03-20 12:29:54.080: E/AndroidRuntime(855):  at java.util.Timer.scheduleAtFixedRate(Timer.java:526)
03-20 12:29:54.080: E/AndroidRuntime(855):  at com.example.bulerias.MainActivity$1.onClick(MainActivity.java:92)
03-20 12:29:54.080: E/AndroidRuntime(855):  at android.view.View.performClick(View.java:4424)
03-20 12:29:54.080: E/AndroidRuntime(855):  at android.view.View$PerformClick.run(View.java:18383)
03-20 12:29:54.080: E/AndroidRuntime(855):  at android.os.Handler.handleCallback(Handler.java:733)
03-20 12:29:54.080: E/AndroidRuntime(855):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-20 12:29:54.080: E/AndroidRuntime(855):  at android.os.Looper.loop(Looper.java:137)
03-20 12:29:54.080: E/AndroidRuntime(855):  at android.app.ActivityThread.main(ActivityThread.java:4998)
03-20 12:29:54.080: E/AndroidRuntime(855):  at java.lang.reflect.Method.invokeNative(Native Method)
03-20 12:29:54.080: E/AndroidRuntime(855):  at java.lang.reflect.Method.invoke(Method.java:515)
03-20 12:29:54.080: E/AndroidRuntime(855):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-20 12:29:54.080: E/AndroidRuntime(855):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-20 12:29:54.080: E/AndroidRuntime(855):  at dalvik.system.NativeStart.main(Native Method)

根据这里的文档,如果scheduleAtFixedRate中有delay < 0period <= 0,则抛出IllegalArgumentException。因此,我假设:period == 0 .
你应该检查你的经期,以确保它不是0:

Timer t = new Timer();
long period = (long) interval*1000;
Log.i("sometag", "period = " period);
t.scheduleAtFixedRate(task, (long) 0, period);

相关内容

  • 没有找到相关文章

最新更新