1

In Android 4.3 this code works. But in android < ~3.0 not working. log:

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()

Code:

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

I want to see this code is repeated every 30 seconds

if(isOnline()){
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://...index.php" });
} else {
    runOnUiThread(successRunnable);
}
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
mr_nobody
  • 194
  • 3
  • 17
  • possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – ajh158 Oct 02 '13 at 13:02
  • Take a look at the Handler example code in this answer: http://stackoverflow.com/a/16886486/273628 – ajh158 Oct 02 '13 at 13:02
  • 1
    task.execute is supposed to run on the thread where you want your onPostExecute to run, and this thread needs a looper for that. Usually, it is the ui thread. – njzk2 Oct 02 '13 at 13:17

1 Answers1

2

It seems that

new DownloadWebPageTask();

contains something that needs to be run on the main UI Thread.

koljaTM
  • 10,064
  • 2
  • 40
  • 42