我试图使用一个调用Class" AysncTask"按固定的时间表。这个计时器/类与GUI或它的元素没有任何关系。在没有计时器的情况下调用该类时它可以工作,但是使用计时器我会得到这些错误:
01-04 08:39:39.074: E/AndroidRuntime(394): FATAL EXCEPTION: main
01-04 08:39:39.074: E/AndroidRuntime(394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projecta/com.example.projecta.MainActivity}: java.lang.RuntimeException: Only one Looper may be created per thread
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Looper.loop(Looper.java:123)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-04 08:39:39.074: E/AndroidRuntime(394): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 08:39:39.074: E/AndroidRuntime(394): at java.lang.reflect.Method.invoke(Method.java:521)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-04 08:39:39.074: E/AndroidRuntime(394): at dalvik.system.NativeStart.main(Native Method)
01-04 08:39:39.074: E/AndroidRuntime(394): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Looper.prepare(Looper.java:73)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.example.projecta.MainActivity.onCreate(MainActivity.java:43)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-04 08:39:39.074: E/AndroidRuntime(394): ... 11 more
到目前为止,这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(),1, 3000);
class UpdateTimeTask extends TimerTask {
public void run()
{
// do stufff
new status().execute();
}
}
那么任何想法如何解决这个问题?并提前感谢
答案 0 :(得分:1)
终于找到了解决方案:
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new status().execute();
// PerformBackgroundTask this class is the class that extends AsynchTask
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 50000 ms
}