我是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 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)
以下是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 :(得分:2)
当您查看错误时,您可能会发现一些"链接"从给出错误的行中,您可以单击并查找。在您的情况下,错误在:
第287行的MainActivity.class:MainActivity.java:287
第19行的MainActivity.class:MainActivity.java:19
第307行的MainActivity.class:MainActivity.java:307
我认为这可能有助于识别错误。
答案 1 :(得分: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();
}