カウントダウンタイマーを実行し、タイマーが切れるとアラートボックスを表示してゲームを再開できるアプリがあります。残念ながら、戻るボタンを押してアプリを再度開くと、元のカウントダウンタイマーが切れるはずだった頃にクラッシュします。
次のコードは、私のアクティビティの 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();