启用设备密码屏幕时游戏崩溃

发布于 2024-10-31 13:13:52 字数 609 浏览 1 评论 0原文

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

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

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

通过打电话或单击“主页”按钮暂停游戏时,我没有任何问题。另外,在另外两台设备上,在游戏过程中关闭和打开它们后,游戏可以正常运行,但它们没有安全屏幕。

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

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

android.graphics.Canvas.throwIfRecycled

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

When using Microsft Exchange, Android activates its password screen wich the user needs to pass every time the device is turned on.

I am making a game which is using SurfaceView run by a thread. I am using many static variables. If the device is turned off while playing, when I come back and enter the password, the game screen shows but with some bitmaps having wrong size and is frozen.

In logs I see first NullPointerException with the non-UI thread then ANR error later. It looks like the turning the device off has destroyed some objects of my application yet it did not go through onCreate and the SurfaceView constructor again when it came back.

I have no problems when pausing the game with a phonecall or clicking HOME button. Also on two other devices the game works fine after turing them off and on in the middle of the play, but they don't have the security screen.

I am using Galaxy Tab, os 2.2

EDIT:
After printing a stacktrace in the thread, I get

android.graphics.Canvas.throwIfRecycled

It seems that some of my bitmaps have been recycled. Any idea how to detect this in onResume or in surfaceChanged() which always fire on returning to the application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望她远 2024-11-07 13:13:52

目前,我对此问题的解决方案是,如果未按照正确的事件顺序恢复该活动,则关闭该活动。如果 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(); 
    }

At the moment my solution to this problem is to close this activity if it is not resumed with the right sequence of events. If onSurfaceChanged happens without onSurfaceCreated happening first, then close this activity. The state of the game can still be preserved and when the player relaunch this activity it will continue where it stopped.

    @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(); 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文