【问题标题】:Can't reenter android application with force close无法通过强制关闭重新进入 android 应用程序
【发布时间】:2010-07-13 20:22:18
【问题描述】:

基本上每次我尝试使用主页按钮退出我的应用程序然后重新进入它时,我都会强制关闭游戏,然后才能打开游戏。当我离开应用程序时,我无法找到如何让它保存状态,我需要它来保存一个数组列表和一些变量和布尔值,但我不知道该怎么做。这是我的活动和我的surfaceCreated、Destroyed 和东西,surfaceCreated 是抛出错误的地方,说线程已经打开。

这里是日志

E/AndroidRuntime(183):未捕获的处理程序:线程主因未捕获而退出 异常 E/AndroidRuntime(183): java.lang.IllegalThreadStateException: 线程已经 s 馅饼。 E/AndroidRuntime(183): 在 java.lang.Thread.start(Thread.java:1286) E/AndroidRuntime(183):在 com.Waldev.cannon.CannonBlast$panel.surfaceCr 吃过(CannonBlast.java:515) E/AndroidRuntime(183): 在 android.view.SurfaceView.updateWindow(Surface 查看.java:392) E/AndroidRuntime(183):在 android.view.SurfaceView.onWindowVisibilityCh anged(SurfaceView.java:182) E/AndroidRuntime(183):在 android.view.View.dispatchWindowVisibilityCha nged(View.java:3745) E/AndroidRuntime(183):在 android.view.ViewGroup.dispatchWindowVisibili tyChanged(ViewGroup.java:690) E/AndroidRuntime(183):在 android.view.ViewGroup.dispatchWindowVisibili tyChanged(ViewGroup.java:690) E/AndroidRuntime(183):在 android.view.ViewRoot.performTraversals(ViewR oot.java:694) E/AndroidRuntime(183): 在 android.view.ViewRoot.handleMessage(ViewRoot. 爪哇:1613) E/AndroidRuntime(183): 在 android.os.Handler.dispatchMessage(Handler.ja 弗吉尼亚州:99) E/AndroidRuntime(183): 在 android.os.Looper.loop(Looper.java:123) E/AndroidRuntime(183): 在 android.app.ActivityThread.main(ActivityThra d.java:4203) E/AndroidRuntime(183): 在 java.lang.reflect.Method.invokeNative(Native 方法) E/AndroidRuntime(183): 在 java.lang.reflect.Method.invoke(Method.java:5 21) E/AndroidRuntime(183):在 com.android.internal.os.ZygoteInit$MethodAndA rgsCaller.run(ZygoteInit.java:791) E/AndroidRuntime(183):在 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 = getWindow();
    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);
}

【问题讨论】:

  • 让我猜猜……你的代码是基于 LunarLauncher 糟糕的例子?
  • 嗯...我在某个地方经历过一些教程,所以也许他们做了,我从那里拿走了我所拥有的并开始为这个工作,现在它实际上几乎是真正的应用程序在工作中有点失败
  • 如果它强制关闭,这意味着您将在日志中有完整的堆栈跟踪。您可能想发布日志。

标签: java android


【解决方案1】:

正如 LogCat 告诉你的那样:“线程已经开始。”

按 HOME 键不会破坏您的活动,因此当您返回应用程序时它可能仍在内存中。当您的活动返回屏幕时,surfaceCreated() 可能再次被调用,导致您尝试再次启动线程......尽管它已经在运行,因为surfaceDestroyed() 没有关闭线程。

我没有玩过 LunarLander 示例代码,也许这种风格适合游戏,但对于一般的 Android 应用,我不推荐这种风格的线程使用。

【讨论】:

  • 嗯,这是一个游戏,我只是说应用程序用于一般用途......但我会尝试弄清楚如何在线程已经存在时阻止它调用surfaceCreated(),谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
相关资源
最近更新 更多