I am very new to Android development and I have created this quiz game. My problem is the app randomly crashes after a few minutes it has been opened. The crashes are at random times so I can't figure out what causes the crashing. Below is the logcat error code whenever the app crashes
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 android.os.BinderProxy@d70d3e4 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)
Here are the codes mentioned at the 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();
}
Thank you!