我在Eclipse中为我的Android应用程序使用scheduleAtFixedRate-timer,但它不起作用:
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);
我认为这很奇怪,并且不知道为什么模拟器会给出一个非法的例子。有人可以帮助我吗?
编辑:堆栈跟踪
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)
答案 0 :(得分:0)
根据文档here,IllegalArgumentException
或delay < 0
中period <= 0
或scheduleAtFixedRate
会引发period == 0
。因此,我假设:Timer t = new Timer();
long period = (long) interval*1000;
Log.i("sometag", "period = " period);
t.scheduleAtFixedRate(task, (long) 0, period);
。
您应该检查您的期间以确保它不是0:
{{1}}