android SurfaceView 转到后台时崩溃
当表面视图通过接收呼叫或退出并返回到应用程序进入后台时,我的应用程序崩溃了。我读到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当返回到之前的活动时,我的应用程序随机(经常)崩溃。
事实证明,“try”语句没有按预期工作,因为 Canvas c 在 getHolder() 方法之后仍然可以为 null。
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.
我认为这部分不是必要的:
如果你使用这个,你需要通知才能释放 wait() 上的锁;
但是尝试使用相同的代码,而不使用同步(mPauseLock)
编辑:
当您按下主页按钮时,应用程序崩溃,问题的解决方案,执行以下操作:
并在 GameView (SurfaceView)中创建构造函数:
I dont think this part is necessary:
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:
And in GameView (SurfaceView) create the constructor: