【问题标题】:Game crashing when device's password screen enabled启用设备的密码屏幕时游戏崩溃
【发布时间】:2011-04-12 15:25:57
【问题描述】:

使用 Microsft Exchange 时,Android 会激活其密码屏幕,用户每次打开设备时都需要通过该密码屏幕。

我正在制作一个使用 SurfaceView 由线程运行的游戏。我正在使用许多静态变量。如果在玩的时候关闭了设备,当我回来输入密码时,游戏画面显示,但有些位图大小错误并被冻结。

在日志中,我首先看到带有非 UI 线程的 NullPointerException,然后是 ANR 错误。看起来关闭设备已经破坏了我的应用程序的一些对象,但是当它回来时它没有再次通过 onCreate 和 SurfaceView 构造函数。

通过电话暂停游戏或单击主页按钮时我没有问题。同样在另外两台设备上,游戏在游戏中途关闭再打开后也能正常运行,但它们没有安全屏幕。

我正在使用 Galaxy Tab,操作系统 2.2

编辑: 在线程中打印堆栈跟踪后,我得到 ​​p>

android.graphics.Canvas.throwIfRecycled

我的一些位图似乎被回收了。知道如何在 onResume 或 surfaceChanged() 中检测到这一点,它总是在返回应用程序时触发?

【问题讨论】:

  • 堆栈跟踪真的很有帮助。您的游戏是否在开机时重新启动?您是否将任何数据保存到 db/file/preferences 中?
  • 在日志中发布您得到的信息,以便我们找出答案

标签: android surfaceview


【解决方案1】:

目前,我对这个问题的解决方案是,如果没有以正确的事件顺序恢复该活动,则关闭该活动。如果 onSurfaceChanged 发生而没有先发生 onSurfaceCreated,则关闭此活动。游戏的状态仍然可以保留,当玩家重新启动这个活动时,它会从停止的地方继续。

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

        //if it does not start through surfaceCreated close activity
       // because some bitmaps could be recycled and crash the application
        if (!surfaceCreatedFirst){
            _thread.setRunning(false); //stop the thread
            ((Activity) context1).finish(); //close activity
        }   

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        surfaceCreatedFirst = true;
        _thread = new FootballThread(holder, this);
        _thread.setRunning(true);
        _thread.start(); 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多