开发者

Force Close caused when exiting during game

开发者 https://www.devze.com 2023-03-09 19:01 出处:网络
Ive tried numerous things to fix this. The most recent being to make the home button and back button do onDestroy(); and finish();

Ive tried numerous things to fix this. The most recent being to make the home button and back button do onDestroy(); and finish();

Basically when I exit my game during game play it exits (the sound still plays for another few moments not sure what to do about that). But then when I click to reenter into playing the game a force close pops up. After it is click it will work again. But I would like to make it when the game is exited at all it destroys everything and makes it a fresh game. Or else it pauses the game when home or back is hit would be fine too if it would prevent the game from force closing in that way. I tried assigning the back and home buttons to pause and I tried the onDestroy(); and finish(); with it as well.

Code used looked like this:

if(keyCode == onKeyDown(int keyCode, KeyEvent event){
    if (k开发者_StackOverflow中文版eyCode == KeyEvent.KEYCODE_BACK){
       onDestroy();
       finish();
     }
    return super.onKeyDown(keyCode, event);
 }

I copied it from a book. The KEYCODE_BACK I also switched for HOME and neither seemed to work.

Also I a tried to use onDestroy(); and finish(); to attempt to reset the game after game over and that wouldnt work. BTW all these had to be coded in the class calling surfaceview because it wouldnt work in the surfaceview.

Any help with these would be appreciated. Thanks

EDIT

public class PlayGame extends Activity{

private static long pauseStartTime;
boolean mute=false;
boolean end = false;
GameView gameView;
Main main;


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new GameView(this));

// if (end){ // onDestroy(); // finish(); // } }

 @Override
 public boolean onCreateOptionsMenu(Menu menu){
   MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
    case R.id.newgame:  
        Intent game = new Intent(PlayGame.this, Main.class);
        startActivity(game);
    break;
    case R.id.pause:

        if(GameView.gameState == 0){
            GameView.gameState=2;
            pauseStartTime = System.currentTimeMillis();

        }
        else if (GameView.gameState == 2){
            GameView.gameState=0;

            long deltaTime = System.currentTimeMillis()-pauseStartTime;

            GameView.spawnZom1Time+=deltaTime;
            GameView.spawnZom2Time+=deltaTime;
            GameView.spawnZom3Time+=deltaTime;
            GameView.spawnSurvTime+=deltaTime;

        }


    break;

    }
    return true;
 }

public boolean onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK){
        onDestroy();
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

}

Thats what it looked like in the code. Honestly I'm garbage at programming so I really dont work with debug cause it just confuses me so not sure how to find the exception you were asking for. And ya I do realize I need to learn to work with debug if anyone is thinking that.

Also if anyone has any input on how to get my mute to work. I tried this and it just wouldnt work.

public class PlayGame extends Activity{

private static long pauseStartTime;
boolean mute=false;
GameView gameView;
Main main;


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new GameView(this));

        mute = main.mute;
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu){
   MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
    case R.id.newgame:  
        Intent game = new Intent(PlayGame.this, Main.class);
        startActivity(game);
    break;
    case R.id.pause:

        if(GameView.gameState == 0){
            GameView.gameState=2;
            pauseStartTime = System.currentTimeMillis();

        }
        else if (GameView.gameState == 2){
            GameView.gameState=0;

            long deltaTime = System.currentTimeMillis()-pauseStartTime;

            GameView.spawnZom1Time+=deltaTime;
            GameView.spawnZom2Time+=deltaTime;
            GameView.spawnZom3Time+=deltaTime;
            GameView.spawnSurvTime+=deltaTime;

        }


    break;
    case R.id.mute: 
        if (mute || main.mute){
            mute = false;
            Toast.makeText(this, "Game Unmuted", Toast.LENGTH_LONG).show();
        }
        else if (mute == false || main.mute == false){
            mute = true;
            Toast.makeText(this, "Game Muted", Toast.LENGTH_LONG).show();
        }
    break;
    }
    return true;
 }

}


You are calling onDestroy() twice, one explicitly and one indirectly with finish();

Remove that line from your method:

if(keyCode == onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK){
       finish();
     }
    return super.onKeyDown(keyCode, event);
 }

Take a look at the dev guide:

onDestroy()

Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Now, re-reading, I'm confused on when you have the exception: is is when you exit (as the title says) or when reloading (as the text seems to suggest)?

In any case, I strongly suggest you to stop coding and spend time reading this: http://developer.android.com/guide/developing/tools/logcat.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号