开发者

Can't reenter android application with force close

Basically every time I try to exit my application using the home button and then reenter it, it makes me force close the game before I can open the game up. I haven't been able to find out how to have it save the state when I leave the app, I need it to save an arraylist and some variables and booleans, but I don't know how to do it. Here is my activity and my surfaceCreated, Destroyed and stuff, the surfaceCreated is where the error is thrown, saying the thread is already open.

Here is the 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)

    @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...
            }
        }
    }

public void onCreate(Bundle savedInstanceState) {
    final Window win = getWi开发者_StackOverflow社区ndow();
    super.onCreate(savedInstanceState);
    panelStuffz = new panel(this);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(panelStuffz);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
}

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


As LogCat tells you: "Thread already started."

Pressing the HOME key does not destroy your activity, so it may still be in memory when you go back into your app. When your activity comes back to the screen, surfaceCreated() is probably being called again, causing you to try to start your thread again...despite the fact that it is already running, since surfaceDestroyed() is not closing down the thread.

I have not played with the LunarLander sample code, and perhaps this style is appropriate for games, but for general Android apps, I would not recommend this style of thread usage.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜