if(pause == null){不起作用

Wie*_*ies 3 java android

我是if(pause == null)用来做什么pausenull.但是我得到了错误

the operator == is undefined for the argument type(s) long,null

这是代码,

public class Timer extends CountDownTimer {
    long pause = (Long) null;

    public Timer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        content.setText("Tijd over: " + millisUntilFinished / 100);
    }

    public void onPause(long millisUntilFinished) {
        if(pause == null) {
            pause = millisUntilFinished;
            content.setText("Tijd over: " + millisUntilFinished / 100);
            this.cancel();
        }
        else {
            this.start();   
        }
    }

    @Override
    public void onFinish() {
        content.setText("Tijd is op!");
    }
}
Run Code Online (Sandbox Code Playgroud)

此类尚未完成,因此请忽略其余代码.

Pri*_*ley 7

long pause = (Long) null;
Run Code Online (Sandbox Code Playgroud)

应该

Long pause = null;
^
Run Code Online (Sandbox Code Playgroud)

long原始类型,但Long是包装对象类型的long.

您可以使用sentinel值而不是将其包装为对象.

long pause = -1;
...
if(pause == -1 ) {
   //do something
}
Run Code Online (Sandbox Code Playgroud)