在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);
}
答案 0 :(得分:2)
似乎
new DownloadWebPageTask();
包含需要在主UI线程上运行的内容。