I am trying to implement a timer and it works fine for the most part..
But for the first time , the clock isn’t starting but for the rest of the activity it is,
The oncreate method has some problem which I am not able to figure out..Help!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends);
Splash.one_time = false;
initilize();
mytime = new Thread() {
public void run() {
timer();
}
};
mytime.start();
mUpdateTimeTask = new Runnable() {
public void run() {
if (count_down > 0) {
if (count_down <= 5)
clock.setTextColor(Color.RED);
else
clock.setTextColor(Color.GREEN);
clock.setText(String.format("%d", count_down--));
mHandler.postDelayed(this, 1000);
} else {
clock.setTextColor(Color.RED);
clock.setText("0");
timeout();
}
}
};
}
private void timer() {
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 0);
}
You are calling
mytime.start();before creatingmUpdateTimeTask, inonCreate(). So the methodtimer()passes an uninitializedmUpdateTimeTasktomHandler.You should initialize first and then start your
Thread: