Alertbox,CountDownTimer,后退按钮崩溃

时间:2012-02-12 17:43:54

标签: android countdowntimer

我有一个运行倒数计时器的应用程序,并在计时器用完时调出警告框,以便游戏可以重启。不幸的是,当我点击后退按钮并再次打开应用程序时,它会在原始倒数计时器耗尽时崩溃。

以下代码位于onCreate of my Activity。

        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();

1 个答案:

答案 0 :(得分:0)

如果没有堆栈跟踪,很难说会发生什么,但很可能有一些事情要从Activity引用死亡CountDownTimer

enter code here中调用onDestroy() CountDownTimer.cancel()可能会解决此问题。