I have this code:
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
public class MActivity extends Activity {
TextView start;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (TextView)findViewById(R.id.start_me);
CountDownTimer aCounter = new CountDownTimer(100000 , 1000) {
public void onTick(long millisUntilFinished) {
start.setText("Remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
start.setText("Finished");
}
};
aCounter.start();
}
}
But when I run it, it crashes with force to close error. Can you help me that where can be the problem?
The
CountDownTimeris not running on the UI Thread and therefore will throw an fatal exception when it tries to modify something that belongs to the UI.How is CountDownTimer accessing UI inside OnTick method?
EDIT:
Seems like @blackbelt commenter was right. I went ahead and made an android project and your code works just fine (Android 2.3.3).
Maybe you’d care to give us the exception? Like an excerpt from your logcat?