我有一个运行倒数计时器的应用程序,并在计时器用完时显示一个警报框,以便游戏可以重新启动。不幸的是,当我点击后退按钮并再次打开应用程序时,它在原来的倒数计时器应该用完的时候崩溃了。
以下代码位于我的 Activity 的 onCreate 中。
CountDownTimer cdt = new CountDownTimer(totalTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
time = (int) ((millisUntilFinished)/1000)*100/totalTime;
TimeBar.setProgress(time);
}
public void onFinish() {
time = 0;
TimeBar.setProgress(time);
AlertDialog.Builder alertbox = new AlertDialog.Builder(mContext);
alertbox.setMessage("Sweet! " + score + " points!");
alertbox.setPositiveButton("Leaderboard", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
ScoreloopManagerSingleton.get().onGamePlayEnded((double) score, null);
startActivity(new Intent(BubblesActivity.this, LeaderboardsScreenActivity.class));
BubblesActivity.this.finish();
}
});
alertbox.setNeutralButton("Replay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
BubblesActivity.this.finish();
startActivity(new Intent(BubblesActivity.this, BubblesActivity.class));
}
});
if(alertbox!= null)
alertbox.show();
}
}.start();