2017-11-16 8 views
0

私はAndroid開発の新機能であり、このクイズゲームを作成しました。私の問題は、アプリが数分開いた後にランダムにクラッシュすることです。クラッシュはランダムに発生するので、クラッシュの原因を突き止めることはできません。以下は、アプリケーションがクラッシュするたびにlogcatエラーコードです。私のアプリが致命的な例外をクラッシュするウィンドウを追加できません

11-16 18:39:10.573 19023-19023/com.noxeternal.quizgame E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: com.noxeternal.quizgame, PID: 19023 
                    android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 
                     at android.view.ViewRootImpl.setView(ViewRootImpl.java:679) 
                     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) 
                     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94) 
                     at android.app.Dialog.show(Dialog.java:322) 
                     at com.noxeternal.quizgame.MainActivity.gameOver(MainActivity.java:287) 
                     at com.noxeternal.quizgame.MainActivity.access$300(MainActivity.java:19) 
                     at com.noxeternal.quizgame.MainActivity$9.onFinish(MainActivity.java:307) 
                     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:154) 
                     at android.app.ActivityThread.main(ActivityThread.java:6176) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

ここにlogcatに記載されているコードがあります。

private void gameOver(){ 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); 
    alertDialogBuilder 
      .setMessage("Game over! Your score is " + mScore + " points.") 
      .setCancelable(false) 
      .setPositiveButton("NEW GAME", 
        new DialogInterface.OnClickListener(){ 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i){ 
          finish(); 
          startActivity(new Intent(getApplicationContext(),MainActivity.class)); 
         } 
        }) 
      .setNegativeButton("Exit", 
        new DialogInterface.OnClickListener(){ 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i){ 
          Intent intent = new Intent(MainActivity.this, MainMenu.class); 
          startActivity(intent); 
          finish(); 
         } 
        }); 
    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
} 

private void startTimer(){ 
    long millisInFuture = 60000; 
    long countDownInterval = 1000; 

    timer.setText("Time remaining: " + timer); 
    countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) { 
     @Override 
     public void onTick(long millisUntilFinished) { 
      if(isPaused){ 
       cancel(); 
      } else { 
       timer.setText("Time remaining: " + millisUntilFinished/1000); 
       remainingTime = millisUntilFinished; 
      } 
     } 
     @Override 
     public void onFinish() { 
      gameOver(); 
     } 
    }; 
    countDownTimer.start(); 
} 

ありがとうございます!

+0

あなたのUIが、活動はdistroyedた更新しようとすると、このエラーは、主に来ます。あなたのコードをここで共有できますか? –

+0

あなたのアクティビティがManifest.xmlに登録されていることを確認してください –

+0

自分の投稿を編集して自分のコードを共有します。君たちありがとう。 –

答えて

1

は、このコードを変更してください:

@Override 
public void onClick(DialogInterface dialogInterface, int i){ 
    finish(); 
    startActivity(new Intent(getApplicationContext(),MainActivity.class)); 
} 

これに:

@Override 
public void onClick(DialogInterface dialogInterface, int i) {   
    startActivity(new Intent(getApplicationContext(),MainActivity.class)); 
    finish(); 
} 
+0

こんにちは。私はこれがトリックだと思う。私はまだクラッシュが起こるかどうか観察しており、皆さんに戻ってきます。ありがとう! –

+0

ようこそ!あなたのアプリがクラッシュしていない場合は、正解を受け入れることができますか? – jibrahim

+0

私のアプリで1時間以上テストボタンとすべてを観察しました。今のところよく見えます。答えとしてマーク。ありがとう! –

2

エラーを見ると、エラーを出す行からいくつかの「リンク」が見つかることがあります。クリックして調べることができます。ライン287上の

MainActivity.class:MainActivity.java:307

I:ライン307上のMainActivity.java:19

MainActivity.class:MainActivity.java:287

MainActivity.classライン19上のあなたのケースでは、エラーがでていますこれがエラーを特定するのに役立つと思う。

関連する問題