我正在尝试使计时器显示在屏幕上。每当我运行该程序时,我都会在2个地方得到NullPointerException。我评论了有错误的行
myCountDownTimer = new MyCountDownTimer(31000, 1000);
myCountDownTimer.onTick(31000); //NullPointerException on this line
myCountDownTimer.onFinish();
这是MyCountDownTimer类
import android.os.CountDownTimer;
import android.widget.TextView;
public class MyCountDownTimer extends CountDownTimer {
TextView textCounter;
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
textCounter.setText(String.valueOf(millisUntilFinished / 1000)); //NullPointerException on this line
}
@Override
public void onFinish() {
textCounter.setText("Finished");
}
}
编辑:
myCountDownTimer = new MyCountDownTimer(31000, 1000);
myCountDownTimer.thisText(textCounter);
myCountDownTimer.onTick(31000);
myCountDownTimer.onFinish();
并将其添加到MyCountDownTimer
public TextView thisText(TextView textCounter){
return textCounter;
}
TextView textCounter;
一片空白; 如果您在活动创造它,然后,创建一个构造函数为你MyCountDownTimer
,并采取TextView
为参数,并把它放在..因为如果你创建一个TextView
在MyCountDownTimer
其无用的,因为你必须重新添加到您的内容查看..
像这样
public class MyCountDownTimer extends CountDownTimer {
TextView textCounter;
public MyCountDownTimer(TextView textCounter, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textCounter = textCounter;
}
// now you can use textCounter and not get an exception
希望能帮助到你
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句