这是一个定时器代码,用于在暂停和重置时保存信息。每次我启动应用程序时,这些信息都会被计时。因为我不想从0开始计时器。我想从上次启动计时器。所以在开始它应该检索最后一次信息。
public class TimerActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences prefs = this.getSharedPreferences("timer", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
timeSwapBuff=prefs.getLong("timeSwapBuff", 0);
startTime=prefs.getLong("startTime", 0);
updatedTime=prefs.getLong("updatedTime", 0);
Log.i("timeSwapBuff", ""+timeSwapBuff);
Log.i("startTime", ""+startTime);
Log.i("updatedTime", ""+updatedTime);
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
// error at this line for set text
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.start);
resetButton = (Button) findViewById(R.id.reset);
pauseButton = (Button) findViewById(R.id.pause);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
editor.putLong("timeSwapBuff", timeSwapBuff);
editor.putLong("startTime", startTime);
editor.putLong("updatedTime", updatedTime);
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime=SystemClock.uptimeMillis();
timeSwapBuff=0;
customHandler.removeCallbacks(updateTimerThread);
timerValue.setText("" + 0 + ":"
+ String.format("%02d", 00) + ":"
+ String.format("%03d", 000));
editor.putLong("timeSwapBuff", timeSwapBuff);
editor.putLong("startTime", startTime);
editor.putLong("updatedTime", updatedTime);
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}
答案 0 :(得分:0)
您在初始化之前将文本设置为 timeValue TextView 。
从
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timerValue = (TextView) findViewById(R.id.timerValue);
到
timerValue = (TextView) findViewById(R.id.timerValue);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
答案 1 :(得分:0)
交换这两个,你在一个未初始化的对象上调用setText。
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timerValue = (TextView) findViewById(R.id.timerValue);
变为
timerValue = (TextView) findViewById(R.id.timerValue);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
答案 2 :(得分:0)
TextView timerValue = (TextView) findViewById(R.id.timerValue);// initialize
String value="" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds);
if(value!=null) //Null Check
timerValue.setText(value);
答案 3 :(得分:0)
您应该在设置值之前初始化textview,如:
timerValue = (TextView) findViewById(R.id.timerValue);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));