我有一个预定的计时器-

public void startTimer(long delay_minutes) {
    final long delay = delay_minutes;
    delay_countup = (double) delay;

    //Start the scheduled time
    departuretimer = new Timer();
    departuretimer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            countup = 0.0 + delay_countup;
            Log.d("hi","Values 0" + delay_countup + countup);
            mHandler.obtainMessage(1).sendToTarget();
            delay_countup = delay_countup + 0.5;
            Log.d("hi","Values 1" + delay_countup);
        }
    }, 0, 30000);
}

public Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Log.d("hi","Values 3" + countup);
            delay_time.setText(String.valueOf(countup) + "min");
            rootView.invalidate();
        }
    };


日志始终显示正确的值,但是在UI中有时会出现问题。问题是,例如-

delay_countup = 50.0
countup = 50.0
Textview updates as 50.0 //This is correct

Now,
delay_countup = 50.5
countup = 50.5
Textview updates as 0.5 //This is incorrect. I need 50.5


有时会发生这种情况。为什么是这样?

编辑:

刚刚也添加了处理程序中的日志...

Values 0  = 34.5 34.5
Values 3  = 0.5

最佳答案

尝试这种方式,将您的mHandler更改为

public Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {

    String result = (String)msg.obj;

        delay_time.setText(result + "min");
        rootView.invalidate();
    }
};




代替mHandler.obtainMessage(1).sendToTarget();使用,

Message message = mHandler.obtainMessage();
message.obj = countup;
mHandler.sendMessage(message);


这可能对您有帮助。

09-27 21:27