开发者

Timer is not implemented the first time

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(savedInst开发者_高级运维anceState);
    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 creating mUpdateTimeTask, in onCreate(). So the method timer() passes an uninitialized mUpdateTimeTask to mHandler.

You should initialize first and then start your Thread:

mUpdateTimeTask = new Runnable() {
   ...
};

mytime.start();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜