【问题标题】:Can't create handler inside thread that has not called Looper.prepare无法在未调用 Looper.prepare 的线程内创建处理程序
【发布时间】:2013-10-02 12:58:04
【问题描述】:

在 Android 4.3 中,此代码有效。 但在 android

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);
}

【问题讨论】:

标签: java android


【解决方案1】:

好像

new DownloadWebPageTask();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-15
    • 2011-09-06
    • 1970-01-01
    相关资源
    最近更新 更多