Force Close caused when exiting during game
<p>Ive tried numerous things to fix this. The most recent being to make the home button and back button do onDestroy(); and finish();</p>

<p>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. </p>

<p>Code used looked like this:</p>

<pre><code>if(keyCode == onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK){
       onDestroy();
       finish();
     }
    return super.onKeyDown(keyCode, event);
 }
</code></pre>

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

<p>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.</p>

<p>Any help with these would be appreciated. Thanks</p>

<p>EDIT</p>

<pre><code>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));
</code></pre>

<p>//          if (end){
//              onDestroy();
//              finish();
//          }
     }</p>

<pre><code> @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);
}
</code></pre>

<p>}</p>

<p>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. </p>

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

<pre><code>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;
 }
</code></pre>

<p>}</p>
