Monday, October 31, 2011

CountDownTimer Error: Can't create handler inside thread that has not called Looper.prepare()

Today I was getting this error "Can't create handler inside thread that has not called Looper.prepare()" in a CountDownTimer. All this time this code used to work perfectly but suddenly it stopped.

Problem is, in CountDownTimer.java (Android source code) it creates a mHandler inside the code which makes the caller should call the CountDownTimer class on UI thread which is not possible in my case. So I created a separate Timer class,

Timer cTimer = new Timer();
private final static int START_FROM = 60;

cTimer.scheduleAtFixedRate(new TimerTask() {
int i = START_FROM ;
public void run() {
Log.d(TAG, "Comparison will start in:" + i--);
if (i< 0) {
cTimer.cancel();
// Do something
}
}
}, 0, 1000);

No comments:

Post a Comment