当游戏结束时,它应该打开一个警告对话,说游戏已经结束,你得到了什么分数。它执行此操作但不止一次,它会一直打开对话框,直到应用程序崩溃。我无法弄清楚它为什么会这样做。 logcat也不会报告错误。我尝试在try {}中放置打开对话框的代码,这使得它有时会工作但不一致。你们是否知道导致问题的原因是什么?
以下是相关代码。
class SnakeView extends SurfaceView implements Runnable {
public SnakeView(Context context, Point size){
super(context);
handler = new Handler(context.getMainLooper());
mContext = context;
mScreenWidth = size.x;
mScreenHeight = size.y;
mBlockSize = mScreenWidth / NUM_BLOCKS_WIDE;
mNumBlocksHigh = ((mScreenHeight)) / mBlockSize;
loadSound();
mHolder = getHolder();
mPaint = new Paint();
mSnakeXs = new int[200];
mSnakeYs = new int[200];
startGame();
}
public void updateGame(){
if (mSnakeXs[0]== mMouseXs && mSnakeYs[0] == mMouseYs){
eatMouse();
}
moveSnake();
if (detectDeath()){
mSoundPool.play(mDead_sound, 1, 1, 0, 0, 1);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder
.setTitle("Game Over!")
.setMessage("Your score: " + mScore + "!")
.setCancelable(false)
.setPositiveButton("New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mContext, SnakeActivity.class);
mContext.startActivity(intent);
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mContext, Start.class);
mContext.startActivity(intent);
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
alertDialogBuilder.show();
}
});
}
}
private void runOnUiThread(Runnable r){
handler.post(r);
}
}
答案 0 :(得分:0)
好吧,我不知道context.getMainLooper()是否正在循环所有内容,或者你的可运行逻辑只是循环回来,但是你可以在类hasdialogBeenShown中声明一个布尔值,在SnakeView中将它设置为false,然后将run()代码设置为:
public void run(){
if(!hasDialogBeenShown){
alertDialogBuilder.show();
hasDialogBeenShown=true;
}
}
这样的事情应该“解决”你的问题,但为什么alertDialogBuilder.show()必须在runnable中呢?为什么它必须runOnUiThread?
答案 1 :(得分:0)
请注意,我只是引入变量dead
来跟踪对话框。这是为了防止程序完成创建新对话框的整个过程,这可能会导致可能导致程序崩溃的OOM异常。
class SnakeView extends SurfaceView implements Runnable {
boolean dead = false;
public SnakeView(Context context, Point size){
super(context);
handler = new Handler(context.getMainLooper());
mContext = context;
mScreenWidth = size.x;
mScreenHeight = size.y;
mBlockSize = mScreenWidth / NUM_BLOCKS_WIDE;
mNumBlocksHigh = ((mScreenHeight)) / mBlockSize;
loadSound();
mHolder = getHolder();
mPaint = new Paint();
mSnakeXs = new int[200];
mSnakeYs = new int[200];
startGame();
}
public void updateGame(){
if (mSnakeXs[0]== mMouseXs && mSnakeYs[0] == mMouseYs){
eatMouse();
}
moveSnake();
if (detectDeath()&& !dead){
mSoundPool.play(mDead_sound, 1, 1, 0, 0, 1);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder
.setTitle("Game Over!")
.setMessage("Your score: " + mScore + "!")
.setCancelable(false)
.setPositiveButton("New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mContext, SnakeActivity.class);
mContext.startActivity(intent);
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mContext, Start.class);
mContext.startActivity(intent);
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
dead = true;
alertDialogBuilder.show();
}
});
}
}
private void runOnUiThread(Runnable r){
handler.post(r);
}
}
答案 2 :(得分:0)
删除此:
runOnUiThread(new Runnable() {
@Override
public void run() {
alertDialogBuilder.show();
}
});
把这个:
final AlertDialog dialog2 = alertDialogBuilder.create();
dialog2.show();