0
所以我有3個活動(LauncherActivity> MenuAvtivity> SelectionActivity) 當我按下後退按鈕從SelectionActivity返回到MenuActivity時,我的應用程序崩潰。ANDROID [後退按鈕導致崩潰]
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用在Canvas線程繪製SurfaceView。
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);
}
}
}
}
感謝您的快速回復。我可以在離開活動時停止線程,並在按下後退按鈕時再次啓動線程。 –
@SanPonko不知道。文件說什麼? – Simon
是的,但不要調用Thread.stop。這是做錯的方法。正確的做法是在Thread的循環頂部檢查一個同步原語 - 通常是一個信號量。在onPause中使用信號量並在onResume中釋放它。然後讓你的線程塊,直到它的循環頂部可用 –