简体   繁体   English

退出活动时Android 4.0上的线程崩溃

[英]Thread crash on Android 4.0 when exiting an Activity

When testing several games with a standard game thread in Android 4.0 it works well until the Activity is shut down (pressing the home button etc), leaving the activity the application crashes with a nullPointer. 在Android 4.0中使用标准游戏线程测试多个游戏时,它可以很好地工作,直到Activity被关闭(按下主屏幕按钮等),从而使该应用程序崩溃并导致nullPointer崩溃。

This even happens in the sample LunarLander that Google has programmed. 这甚至发生在Google编写的示例LunarLander中。

The problem is that Canvas becomes null when leaving the activity and that makes the application crash. 问题在于,Canvas在离开活动时变为空,这使应用程序崩溃。

The error message from the LogCat is just below. LogCat的错误消息在下面。

02-27 18:07:58.974: V/MainThread(2667): CANVAS android.view.Surface$CompatibleCanvas@4102bcf0
02-27 18:07:59.164: V/MainThread(2667): CANVAS null
02-27 18:07:59.164: W/dalvikvm(2667): threadid=14: thread exiting with uncaught exception (group=0x409c01f8)
02-27 18:07:59.174: E/AndroidRuntime(2667): FATAL EXCEPTION: Thread-108
02-27 18:07:59.174: E/AndroidRuntime(2667): java.lang.NullPointerException
02-27 18:07:59.174: E/AndroidRuntime(2667):     at com.joakimengstrom.pong.MainThread.run(MainThread.java:49)

This is the code when starting a thread, with the Log.v you see above. 这是启动线程时使用上面看到的Log.v的代码。

    while(this.running){
        canvas = null;

        try {               
            canvas = this.surfaceHolder.lockCanvas(null);
            synchronized (surfaceHolder) {
                Log.v(TAG, "CANVAS " + canvas);
                canvas.drawColor(Color.BLACK);
                ...

And below is when creating the thread and shutting it down when the surface is destroyed. 下面是创建线程并在表面被破坏时将其关闭的时间。

@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread = new MainThread(getHolder());
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    thread.setRunning(false);
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {

        }
    }
}

How do I exit the thread in a safe way without making canvas null? 如何以安全的方式退出线程而不使canvas为空?

In Android 4.0, SurfaceHolder.lockCanvas can return null when the surface is destroyed. 在Android 4.0中, SurfaceHolder.lockCanvas表面时SurfaceHolder.lockCanvas可以返回null So here: 所以在这里:

try {               
            canvas = this.surfaceHolder.lockCanvas(null);
            synchronized (surfaceHolder) {
                Log.v(TAG, "CANVAS " + canvas);
                canvas.drawColor(Color.BLACK);

Just surround your synchronized (surfaceHolder) block with if (canvas != null) . 只需在您的synchronized (surfaceHolder)块周围加上if (canvas != null) This should not cause any issues with the behaviour of your application as it occurs when drawing is not needed. 这不会引起应用程序行为的任何问题,因为不需要绘图时会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM