android SurfaceView 转到后台时崩溃

发布于 2024-12-11 03:08:59 字数 4475 浏览 0 评论 0原文

当表面视图通过接收呼叫或退出并返回到应用程序进入后台时,我的应用程序崩溃了。我读到 Surfacedestroyed 在这些情况下不会接到电话,但是 给出的解决方案对我不起作用。 如果有人可以帮助我编写我的代码,我将不胜感激。 谢谢,

public GameLoopThread(GameView view)
{

this.view=view;
}
public void setRunning (boolean run)
{
    running=run;
}
@Override
public void run() {

    long ticksPerSecond=1000/FPS;
    long startTime;
    long sleepTime;
    while (running)
    {
        Canvas c=null;
        startTime=System.currentTimeMillis();
        try {
        c=view.getHolder().lockCanvas();
        synchronized (view.getHolder()) {
            view.onDraw(c);
        }

        } catch (Exception e) {
            // TODO: handle exception
        }
        finally{
            if(c!=null)
            view.getHolder().unlockCanvasAndPost(c);
        }
        sleepTime=ticksPerSecond-(System.currentTimeMillis()-startTime);
        try
        {if(sleepTime>0)
            sleep(sleepTime);
        else
            sleep(10);

        }
        catch(Exception e){}

        synchronized (mPauseLock) {
            while (!running) {
                try {
                    mPauseLock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }

and in the surfaceview:
 public void surfaceDestroyed(SurfaceHolder holder) {
 if(gameLoopThread.isAlive())
            {
                boolean retry = true;
                gameLoopThread.setRunning(false);
                while (retry) {
                    try {
                        gameLoopThread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                    }
                }
            }
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if(!gameLoopThread.isAlive())
                    {           

                     gameLoopThread.setRunning(true);
                     gameLoopThread.start();

                    }
            }

也许 logcat 会有所帮助:

10-22 09:35:06.310: 调试/好友---------------------------->(10985): 服务: OnReceive ACTION_HOME_RESUME 调用 10-22 09:35:06.315:DEBUG/AndroidRuntime(3275):关闭虚拟机 10-22 09:35:06.315: WARN/dalvikvm(3275): threadid=1: 线程因未捕获的异常而退出(group=0x4001e578) 10-22 09:35:06.315:错误/AndroidRuntime(3275):致命异常:主要 10-22 09:35:06.315: 错误/AndroidRuntime(3275): java.lang.NullPointerException 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 tomer.idunku3.GameView$1.surfaceDestroyed(GameView.java:126) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:613) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.SurfaceView.updateWindow(SurfaceView.java:498) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.SurfaceView.updateWindow(SurfaceView.java:407) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:217) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.View.dispatchWindowVisibilityChanged(View.java:4080) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.ViewRoot.performTraversals(ViewRoot.java:790) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.view.ViewRoot.handleMessage(ViewRoot.java:1868) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.os.Handler.dispatchMessage(Handler.java:99) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.os.Looper.loop(Looper.java:123) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 android.app.ActivityThread.main(ActivityThread.java:3691) 10-22 09:35:06.315:错误/AndroidRuntime(3275):在java.lang.reflect.Method.invokeNative(本机方法) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 java.lang.reflect.Method.invoke(Method.java:507) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 10-22 09:35:06.315: 错误/AndroidRuntime(3275): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 10-22 09:35:06.315:错误/AndroidRuntime(3275):在dalvik.system.NativeStart.main(本机方法) 10-22 09:35:06.320:错误/(18080):转储状态> /数据/日志/dumpstate_app_error

my app get crashed when the surfaceview goes to background by recieving call or exit and return to the app. i read that surfacedestroyed doesnt get call on these situations but
the solutions that were given didnt work for me.
i will be gratefull if someone can help me with my code.
thanks

public GameLoopThread(GameView view)
{

this.view=view;
}
public void setRunning (boolean run)
{
    running=run;
}
@Override
public void run() {

    long ticksPerSecond=1000/FPS;
    long startTime;
    long sleepTime;
    while (running)
    {
        Canvas c=null;
        startTime=System.currentTimeMillis();
        try {
        c=view.getHolder().lockCanvas();
        synchronized (view.getHolder()) {
            view.onDraw(c);
        }

        } catch (Exception e) {
            // TODO: handle exception
        }
        finally{
            if(c!=null)
            view.getHolder().unlockCanvasAndPost(c);
        }
        sleepTime=ticksPerSecond-(System.currentTimeMillis()-startTime);
        try
        {if(sleepTime>0)
            sleep(sleepTime);
        else
            sleep(10);

        }
        catch(Exception e){}

        synchronized (mPauseLock) {
            while (!running) {
                try {
                    mPauseLock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }

and in the surfaceview:
 public void surfaceDestroyed(SurfaceHolder holder) {
 if(gameLoopThread.isAlive())
            {
                boolean retry = true;
                gameLoopThread.setRunning(false);
                while (retry) {
                    try {
                        gameLoopThread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                    }
                }
            }
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if(!gameLoopThread.isAlive())
                    {           

                     gameLoopThread.setRunning(true);
                     gameLoopThread.start();

                    }
            }

maybe the logcat will help:

10-22 09:35:06.310: DEBUG/Buddies--------------------------->(10985): Service:OnReceive ACTION_HOME_RESUME called
10-22 09:35:06.315: DEBUG/AndroidRuntime(3275): Shutting down VM
10-22 09:35:06.315: WARN/dalvikvm(3275): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): FATAL EXCEPTION: main
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): java.lang.NullPointerException
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at tomer.idunku3.GameView$1.surfaceDestroyed(GameView.java:126)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:613)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.SurfaceView.updateWindow(SurfaceView.java:498)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.SurfaceView.updateWindow(SurfaceView.java:407)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:217)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.View.dispatchWindowVisibilityChanged(View.java:4080)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.ViewRoot.performTraversals(ViewRoot.java:790)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.os.Looper.loop(Looper.java:123)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-22 09:35:06.315: ERROR/AndroidRuntime(3275): at dalvik.system.NativeStart.main(Native Method)
10-22 09:35:06.320: ERROR/(18080): Dumpstate > /data/log/dumpstate_app_error

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

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

发布评论

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

评论(2

咋地 2024-12-18 03:08:59

当返回到之前的活动时,我的应用程序随机(经常)崩溃。
事实证明,“try”语句没有按预期工作,因为 Canvas c 在 getHolder() 方法之后仍然可以为 null。

while (running) {
    Canvas c=null;
    startTime=System.currentTimeMillis();
    try {
        c=view.getHolder().lockCanvas();
        if(c!=null){ // <- this is the line of code I had to add to make it work
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        }
    } catch (Exception e) {}
    finally{
        if(c!=null)
        view.getHolder().unlockCanvasAndPost(c);
    }
    //...
}

My application randomly (pretty often) crashed when returning to previous activity.
Turns out the "try" statement is not working as expected as Canvas c can be null still after the getHolder() method.

while (running) {
    Canvas c=null;
    startTime=System.currentTimeMillis();
    try {
        c=view.getHolder().lockCanvas();
        if(c!=null){ // <- this is the line of code I had to add to make it work
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        }
    } catch (Exception e) {}
    finally{
        if(c!=null)
        view.getHolder().unlockCanvasAndPost(c);
    }
    //...
}
酷炫老祖宗 2024-12-18 03:08:59

我认为这部分不是必要的:

synchronized (mPauseLock) {
        while (!running) {
            try {
                mPauseLock.wait();
            } catch (InterruptedException e) {
            }
        }
    }

如果你使用这个,你需要通知才能释放 wait() 上的锁;
但是尝试使用相同的代码,而不使用同步(mPauseLock)

编辑:

当您按下主页按钮时,应用程序崩溃,问题的解决方案,执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    setContentView(gameView); 
}

@Override
public void onPause(){
    super.onPause();
    gameView.gameLoopThread.setRunning(false);
    finish();
}

并在 GameView (SurfaceView)中创建构造函数:

public GameLoopThread gameLoopThread;

public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);
}

I dont think this part is necessary:

synchronized (mPauseLock) {
        while (!running) {
            try {
                mPauseLock.wait();
            } catch (InterruptedException e) {
            }
        }
    }

If you use this, you'll need to notify in order to release the lock on wait();
But try using the same code, with out the synchronized(mPauseLock)

EDIT:

The solution for your problem when you press on the Home button, the application crashes, do this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    setContentView(gameView); 
}

@Override
public void onPause(){
    super.onPause();
    gameView.gameLoopThread.setRunning(false);
    finish();
}

And in GameView (SurfaceView) create the constructor:

public GameLoopThread gameLoopThread;

public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文