在java中使用计时器错误

时间:2017-03-29 17:13:01

标签: java

 public void setTimerCount(View view){
        int value5;
        final double timerCountSeconds = 0;

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                timerCountSeconds ++ 0.1;
                timerCount.setText("" + (timerCountSeconds));

            }
        };

我认为这是正确的但我在运行函数调用变量时出现此错误"不是语句

2 个答案:

答案 0 :(得分:0)

试试这个..

public void setTimerCount(View view){
    int value5;
    final double timerCountSeconds = 0;

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            //timerCountSeconds ++ 0.1; is not valid java syntax
            timerCountSeconds =timerCountSeconds+ 0.1;
            timerCount.setText("" + (timerCountSeconds));

        }
    };

答案 1 :(得分:0)

final double timerCountSeconds = 0;

您正在尝试更改最终变量的值...

timerCountSeconds ++ 0.1;

您可能打算使用timerCountSeconds += 0.1;

试试这段代码:

public void setTimerCount(View view) {
    int value5;
    final double[] timerCountSeconds = {0};

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            timerCountSeconds[0] += timerCountSeconds[0] + 0.1;
            timerCount.setText("" + (timerCountSeconds[0]));

        }
    };
}