应用程序崩溃,在Android 4.0,而不是2.3 - 斯特兰奇异常(App Crashes o

2019-09-19 18:58发布

我正在开发的2.3的安卓游戏,并在更多的设备开始测试,这样我可以释放它。

在某些,并非所有4.0的设备我的游戏崩溃最小化时。 您可以锁定手机和解锁,游戏重新开始,并暂停就好了。 当您尝试返回到主屏幕发生崩溃。

错误日志显示如下:

07-18 14:33:44.839 E/AndroidRuntime(15542)FATAL EXCEPTION: Thread-662
07-18 14:33:44.839 E/AndroidRuntime(15542)java.lang.NullPointerException
07-18 14:33:44.839 E/AndroidRuntime(15542)at com.petronicarts.stormthecastle.MainThread.run(MainThread.java:55)
07-18 14:33:44.846 W/IInputConnectionWrapper(15542)showStatusIcon on inactive InputConnection
07-18 14:33:45.081 I/ActivityManager(178)No longer want com.android.packageinstaller (pid 15351): hidden #16
07-18 14:33:45.143 W/InputDispatcher(178)channel '41bb4d28 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
07-18 14:33:45.143 E/InputDispatcher(178)channel '41bb4d28 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress (server)' ~ Channel is unrecoverably broken and will be disposed!
07-18 14:33:45.190 W/InputDispatcher(178)Attempted to unregister already unregistered input channel '41bb4d28 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress (server)'
07-18 14:33:45.190 I/WindowManager(178)WIN DEATH: Window{41bb4d28 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress paused=false}
07-18 14:33:45.198 I/WindowManager(178)WINDOW DIED Window{41bb4d28 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress paused=false}
07-18 14:33:47.268 D/dalvikvm(178)GC_EXPLICIT freed 338K, 20% free 22816K/28487K, paused 6ms+5ms
07-18 14:34:15.464 I/power   (178)*** set_screen_state 0
07-18 14:34:15.471 D/SurfaceFlinger(115)About to give-up screen, flinger = 0x1822918
07-18 14:34:15.596 D/NfcService(403)NFC-C OFF, disconnect

我认为重要的线路是这样的:

com.petronicarts.stormthecastle.MainThread.run(MainThread.java:55)

纵观55行,那就是:

canvas.setMatrix(matrix);

我运行的线程中:

    @Override
public void run() 
{
    boolean ScaleGame = true;
    //boolean SkipFrame = false;
    Bitmap screen = Bitmap.createBitmap(960, 540, Config.RGB_565);
    Canvas canvas;
    Canvas canvas2 = new Canvas(screen);
    Paint paint = new Paint();
    Matrix matrix = new Matrix();
    matrix.preScale(gamePanel.getScaleX(), gamePanel.getScaleY());
    if (gamePanel.getScaleX() == 1 && gamePanel.getScaleY() == 1)
        ScaleGame = false;
    long startTime, elapsedTime;
    startTime = System.currentTimeMillis();
    elapsedTime = System.currentTimeMillis() - startTime;

    this.gamePanel.setScreenBitmap(screen);

    while (running) {
        if(!pleaseWait) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                if (ScaleGame)
                    canvas.setMatrix(matrix);
                synchronized (surfaceHolder) {
                    startTime = System.currentTimeMillis();
                    this.gamePanel.update((float)elapsedTime);

                    canvas.drawBitmap(screen, 0, 0, paint);
                    elapsedTime = System.currentTimeMillis() - startTime;

                }
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }            
        }
        else {
            synchronized (this) {
                try {
                    wait();
                } catch (Exception e) { }
            }
        }
    }
}

我不知道为什么我会无法使基质。 这似乎是代码应该只是罚款。 我认为,这意味着我在处理,仅适用于2.3个什么东西。

我的暂停,恢复和销毁事件如下所示:

public void pause() {
    justPause = true;
    pauseGame = true;


    SharedPreferences fileStore = this.getContext().getSharedPreferences("userData", 0);
    SharedPreferences.Editor editor = fileStore.edit();
    editor.putInt("highscore", highscore);
    editor.commit();

    AudioService.StopMusic();

}

public void resume(Context context) {
    if (gameState == 1)
        AudioService.StartMusic();
    //gold += 1000;
}

public void destroy() {
    thread.setRunning(false);


    if (thread != null)
    {
        Thread killThread = thread;
        thread = null;
        killThread.interrupt();
    }   
}

如果您有任何意见,他们会非常欢迎。 谢谢。

Answer 1:

有一个问题我看到的是调用后“画布= this.surfaceHolder.lockCanvas();” 你只检查空finally块。 我要改变它看起来像这样:

if(!pleaseWait) {
    canvas = this.surfaceHolder.lockCanvas();

    if (canvas != null) {
        // try locking the canvas for exclusive pixel editing on the surface
        try {
            if (ScaleGame)
                canvas.setMatrix(matrix);
            synchronized (surfaceHolder) {
                startTime = System.currentTimeMillis();
                this.gamePanel.update((float)elapsedTime);
                canvas.drawBitmap(screen, 0, 0, paint);
                elapsedTime = System.currentTimeMillis() - startTime;
        }
        finally {
            surfaceHolder.unlockCanvasAndPost(canvas);
        }            
    }
}

我还要问,如果在画布矩阵没有设置同步块应该运行(时ScaleGame是假的)。 我没有足够的洞察问题或帆布为了回答这个问题。 如果答案为否,则同步块应在if语句包裹。



Answer 2:

你永远不应该持有的UI线程上的锁,因为它可以阻止用户界面线程,并防止用户与您的应用程序顺利进行交互。 对于文档lockCanvas建议用一个单独的引出线,而不是同步:

如果没有返回null, lockCanvas()内部保持锁定直到相应unlockCanvasAndPost(Canvas)呼叫,阻止SurfaceView从创建,销毁或修改表面,同时正在绘制它。 这可能是比访问更方便的Surface直接,因为你并不需要做的引出线特殊的同步Callback.surfaceDestroyed

Android 3.0以上版本介绍StrictMode检查,以确保开发商没有滥用UI线程,所以这是毫不奇怪,你的应用程序适用于Android 2.3,但不是在Android 4.0。



文章来源: App Crashes on Android 4.0, but not 2.3 — Strange Exception