1

在 Android 4.3 中,此代码有效。但在 android < ~3.0 中不起作用。日志:

10-02 00:10:55.265: ERROR/AndroidRuntime(356): FATAL EXCEPTION: Timer-0
    java.lang.ExceptionInInitializerError
    at ru.cl.radio.MyActivity$1$1.run(MyActivity.java:129)
    at java.util.Timer$TimerImpl.run(Timer.java:284)
    Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

代码:

Timer timer = new Timer();
TimerTask hourlyTask = new TimerTask () {
    @Override
    public void run () {
        if(isOnline()){
            DownloadWebPageTask task = new DownloadWebPageTask(); // **129 line!!!**
            task.execute(new String[] { "http://.../index.php" });
        } else{
            runOnUiThread(successRunnable);
        }
    }
};

timer.schedule (hourlyTask, 0l, 1000*30);

我想看到这段代码每 30 秒重复一次

if(isOnline()){
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://...index.php" });
} else {
    runOnUiThread(successRunnable);
}
4

1 回答 1

2

看起来

new DownloadWebPageTask();

包含需要在主 UI 线程上运行的内容。

于 2013-10-02T13:36:08.870 回答