• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Issues with reopening game after close

Panthersgba

Newbie
Jan 5, 2010
16
0
Hey guys,
I have been working on a game, but I can't seem to get it to reopen where it leaves off if you hit the home button. I don't know how I need to set up the onCreate and stuff. I had put in a few things, but it keeps force closing when I try to open it back up and gives me this logcat

E/AndroidRuntime( 183): Uncaught handler: thread main exiting due to uncaught e xception E/AndroidRuntime( 183): java.lang.IllegalThreadStateException: Thread already s tarted. E/AndroidRuntime( 183): at java.lang.Thread.start(Thread.java:1286) E/AndroidRuntime( 183): at com.Waldev.cannon.CannonBlast$panel.surfaceCr eated(CannonBlast.java:515) E/AndroidRuntime( 183): at android.view.SurfaceView.updateWindow(Surface View.java:392) E/AndroidRuntime( 183): at android.view.SurfaceView.onWindowVisibilityCh anged(SurfaceView.java:182) E/AndroidRuntime( 183): at android.view.View.dispatchWindowVisibilityCha nged(View.java:3745) E/AndroidRuntime( 183): at android.view.ViewGroup.dispatchWindowVisibili tyChanged(ViewGroup.java:690) E/AndroidRuntime( 183): at android.view.ViewGroup.dispatchWindowVisibili tyChanged(ViewGroup.java:690) E/AndroidRuntime( 183): at android.view.ViewRoot.performTraversals(ViewR oot.java:694) E/AndroidRuntime( 183): at android.view.ViewRoot.handleMessage(ViewRoot. java:1613) E/AndroidRuntime( 183): at android.os.Handler.dispatchMessage(Handler.ja va:99) E/AndroidRuntime( 183): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 183): at android.app.ActivityThread.main(ActivityThrea d.java:4203) E/AndroidRuntime( 183): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 183): at java.lang.reflect.Method.invoke(Method.java:5 21) E/AndroidRuntime( 183): at com.android.internal.os.ZygoteInit$MethodAndA rgsCaller.run(ZygoteInit.java:791) E/AndroidRuntime( 183): at com.android.internal.os.ZygoteInit.main(Zygot eInit.java:549) E/AndroidRuntime( 183): at dalvik.system.NativeStart.main(Native Method)

I am not sure exactly what to do with what I have to make it work, I just know that the thread is still running and I need to keep surfaceCreated from going I guess. Here are my onCreate and surface stuff

public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}

public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_MENU){
if(panelStuffz.getState() != -11){
panelStuffz.setPreviousState(panelStuffz.getState());
panelStuffz.setState(-11);
return true;
}
if(panelStuffz.getState() == -11){
panelStuffz.setState(panelStuffz.getPreviousState());
}
}
return false;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}

any ideas? My friends and I have been stumped : /
 
Hi Panthersgba

For future reference, could you post code and log lines in CODE tags please, so that indentation & layout are preserved and they're easier to read.

It's hard to know if this will be relevant in your case, but do you handle onPause and onResume? In my game that's where I stop my game thread and start it again, and deal with other resource loading activities.

Code:
    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        m_GLSurfaceView.onResume();

        GameSound.loadSounds(this) ;
        if( m_gameStarted ) {
            m_gameThread = new GameThread(m_game) ;
            m_gameThread.start() ;        	
        }
        
        startSensor() ;
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        m_GLSurfaceView.onPause();
        GameSound.releasePool() ;

        if( m_gameThread!=null ) {
            m_gameThread.stopGame() ;
            m_gameThread = null ;        	
        }
        
        stopSensor() ;
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
        GameSound.releasePool() ;

    	if( m_gameThread!=null ) {
            m_gameThread.stopGame() ;
            m_gameThread = null ; 
            m_gameStarted = false ;
        }
    }

Just a suggestion.

Mark
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones