I'm trying to implement a Countdown Timer which shows on the UI, I thought this was a clean and elegant solution but Logcat throws
AndroidRuntime(3282): java.lang.RuntimeException: An error occured while executing doInBackground()
,
private class UpdateCountdownTask extends AsyncTask<Long, Long, Void> {
@Override
protected Void doInBackground(Long... millisUntilFinished) {
new CountDownTimer(millisUntilFinished[0], 1000) {
@Override
public void onTick(long millisUntilFinished) {
publishProgress(millisUntilFinished);
}
@Override
public void onFinish() {
((TextView) findViewById(R.id.answer)).setText(answer);
}
};
return null;
}
@Override
protected void onProgressUpdate(Long... millisUntilFinished) {
super.onProgressUpdate(millisUntilFinished);
((TextView) findViewById(R.id.timer)).setText(""
+ millisUntilFinished[0] / 1000);
}
}
Still don't fully understand threads. Can anyone tell me what I'm doing wrong?
Thanks.
UPDATE: Very Sorry. I am an absolut incompetent. I had forgotten to call the metod start() on the counter. :P I finally went for implementing the countdown with the CountDownTimer by itself. Sill don't know if this actually runs on a separate thread or not but it works now.
CountDownTimer myCounter = new CountDownTimer(millisUntilFinished, 1000) {
@Override
public void onTick(long millisUntilFinished) {
((TextView) findViewById(R.id.timer)).setText(""
+ millisUntilFinished / 1000);
System.out.println("I am ticking");
}
@Override
public void onFinish() {
}
};
myCounter.start();