ANDROID [后退按钮导致崩溃]

时间:2014-12-21 19:48:19

标签: java android eclipse multithreading android-intent

所以我有3个活动(LauncherActivity> MenuAvtivity> SelectionActivity) 当我按后退按钮从SelectionActivity返回到MenuActivity时,我的应用程序会崩溃。

Code: Intent intent = new Intent(MenuActivity.this,SelectionActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent); 

错误代码:

12-21 20:37:52.179: E/AndroidRuntime(14823): FATAL EXCEPTION: main
12-21 20:37:52.179: E/AndroidRuntime(14823): Process: com.example.myapp, PID: 14823
12-21 20:37:52.179: E/AndroidRuntime(14823): java.lang.IllegalThreadStateException: Thread already started
12-21 20:37:52.179: E/AndroidRuntime(14823):    at java.lang.Thread.checkNotStarted(Thread.java:871)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at java.lang.Thread.start(Thread.java:1025)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at com.example.myapp.MenuView$1.surfaceCreated(MenuView.java:51)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:256)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.View.dispatchWindowVisibilityChanged(View.java:8096)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1448)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.Choreographer.doFrame(Choreographer.java:560)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.os.Handler.handleCallback(Handler.java:808)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.os.Handler.dispatchMessage(Handler.java:103)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.os.Looper.loop(Looper.java:193)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at android.app.ActivityThread.main(ActivityThread.java:5292) 
12-21 20:37:52.179: E/AndroidRuntime(14823):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at java.lang.reflect.Method.invoke(Method.java:515)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
12-21 20:37:52.179: E/AndroidRuntime(14823):    at dalvik.system.NativeStart.main(Native Method)

LauncherActivity使用SurfaceView,MenuActivity使用SurfaceView,并在Canvas上使用Thread绘图。

线程类:

@Override
public void run() {
    while (running) {
        Canvas c = null;
        try {
            c = menuView.getHolder().lockCanvas();
            synchronized (menuView.getHolder()) {
                menuView.doDraw(c);
            }
        } finally {
            if (c != null) {
                menuView.getHolder().unlockCanvasAndPost(c);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

.IllegalThreadStateException:线程已经启动

所以在你的surfaceCreated中,你正在调用thread.start。但是该线程之前已经启动过。所以要么不重新启动它,要么创建一个新线程。我的猜测是第一个是正确的答案,但没有代码,我无法确定。