LG G4在基于Lunar Lander的Android游戏开始时崩溃

时间:2016-01-21 23:03:51

标签: java android crash jvm-crash lg

我根据Google的Lunar Lander示例编写了一款Android游戏(使用Android Canvas进行2D绘图)。自2009年以来,该游戏在所有Android设备上运行良好,除了用户最近报道它无法在LG G4上启动。有时G4完全冻结,因此用户必须取出电池才能重新启动它。

我使用LG的开发者程序借用LG G4,并重现了这个问题。基本上应用程序暂停,没有任何例外。通常它最终会给出一个没有响应的应用程序"错误。我还观察到操作系统偶尔会完全崩溃,并且很少会成功开始游戏。

我找到了解决问题的解决方法,所以我在下面回答我自己的问题,以防其他人遇到此问题。 (虽然真正的解决方案是LG不会在Android操作系统中引入错误。)

1 个答案:

答案 0 :(得分:-1)

我进行了"二分搜索"通过添加日志语句来查找程序冻结的行。我发现在初始启动时,当渲染线程试图获取画布上的锁时,会发生冻结。

如果我在渲染线程尝试获取锁之前插入thread.sleep(),问题就会消失。我完全意外地发现了这个:我在那里插入了一个日志语句,并且日志语句本身引起了足够的延迟,游戏开始工作!这是我的(简化)代码结构和修复:

public class theview extends SurfaceView implements SurfaceHolder.Callback {
    private final SurfaceHolder mSurfaceHolder;
    public theview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mSurfaceHolder = getHolder();
        //(other game-initialization code omitted)
        thread = new LunarThread();

    public void surfaceCreated(SurfaceHolder holder) {
        //(other game-resuming code omitted)
        mRun=True;
        thread.start();
    }

    class LunarThread extends Thread {
        @Override
        public void run() {
            Canvas c=null;
            while (mRun) {
                try {
                    LunarThread.sleep(0, 1); // <---the LG G4 needs this line or the game doesn't load! Sleep duration didn't matter.
                }catch(InterruptedException ignored){}

                if(c==null){
                    LunarThread.yield();//app is still loading; wait for it.
                }
                try {
                    c = mSurfaceHolder.lockCanvas(null); // <---- this is the line where the LG G4 freezes without the above sleep()
                    if (c != null) {
                        synchronized (mSurfaceHolder) {
                            if (mMode == STATE_RUNNING) doPhysics();
                            doDraw(c);
                        }
                    }
                }
                finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }

}

1ns延迟足够短,我刚刚为所有手机添加了它。

(不用说,但显然这只是对LG G4定制版Android操作系统中一个令人讨厌的底层错误的缓解。客户报告说,其他LG并不会发生这种情况。我将这个问题单独提交给LG。)