在这里,我有这个 CountDownTimer,我想在无限循环中启动和重新启动,但我不知道为什么它不起作用:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
xtime = System.currentTimeMillis()-prefs.getLong("time",System.currentTimeMillis());
timer=24000-(xtime+prefs.getLong("time2",0));
final SharedPreferences.Editor editor = prefs.edit();
editor.putLong("time2",xtime+prefs.getLong("time2",0));
editor.commit();
new CountDownTimer(timer, 1000) {
public void onTick(long elapsed) {
long timer2=elapsed;
hours = timer2 / hours_in_millies;
timer2 %= hours_in_millies;
minutes = timer2 / minutes_in_millies;
timer2 %= minutes_in_millies;
Log.v(TAG, "seconds" + seconds);
if(seconds<10)
seconds=timer2/1000;
else
seconds = timer2 / seconds_in_millies;
if(seconds<10)
seconds=timer2/100;
if(seconds>1&&seconds<2)
seconds=0;
if(hours>=10&&minutes>=10&&seconds>=10)
time.setText(hours + ":" + minutes + ":" + seconds);
else
if (hours<10&&minutes>=10&&seconds>=10)
time.setText("0"+hours + ":" + minutes + ":" + seconds);
else
if (hours<10&&minutes<10&&seconds>=10)
time.setText("0"+hours + ":0" + minutes + ":" + seconds);
else
if (hours>=10&&minutes<10&&seconds<10)
time.setText("0"+hours + ":0" + minutes + ":0" + seconds);
else
if(hours>=10&&minutes>=10&&seconds<10)
time.setText(hours + ":" + minutes + ":0" + seconds);
else
if (hours>=10&&minutes<10&&seconds<10)
time.setText(hours + ":0" + minutes + ":0" + seconds);
else
if(hours>=10&&minutes<10&&seconds>=10)
time.setText(hours + ":0" + minutes+":" + seconds);
else if(hours<10&&minutes>=10 && seconds<10)
time.setText("0"+hours + ":" + minutes +":0"+ seconds);
}
public void onFinish()
{ editor.clear();
this.start();
}
}.start();
xtime=System.currentTimeMillis();
SharedPreferences.Editor editor2 = prefs.edit();
editor2.putLong("time",xtime);
editor2.commit();
}
SharedPreferences 帮助我使用我的算法从 Activity 结束的那一点恢复 CountDownTimer。问题是,即使我在我的 OnFinish() 中调用 this.start(),我在 this.start 的行上有这个 NullPointerException 错误。我会很感激一些帮助。谢谢!PS:当计时器低于 10 秒时,我也遇到问题,之后出现以下数字,但有一个奖励数字。(95,85,75...15)。如果在我的:
if(seconds<10)
seconds=timer2/1000;
else
seconds = timer2 / seconds_in_millies;
我会用 timer2/10000 更改 seconds=timer2/1000 它根本不显示数字并在 10 处停止。