gpt4 book ai didi

java - ANDROID [后退按钮导致崩溃]

转载 作者:行者123 更新时间:2023-12-01 12:06:01 44 4
gpt4 key购买 nike

所以我有 3 个 Activity (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 上绘制线程。

线程类:

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

最佳答案

.IllegalThreadStateException:线程已启动

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

关于java - ANDROID [后退按钮导致崩溃],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593114/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com