我有预定的计时器 -
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
答案 0 :(得分:1)
尝试这种方式,将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);
这可能会对你有所帮助。