Android surface view crashes when moving to the background

my application crashes when the surface view fades into the background, getting a call or exiting and returning to the application. I read that surfacedestroyed does not get a call in these situations, but the solutions that were given did not help me. I will appreciate 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 logcat will help:

10-22 09: 35: 06.310: DEBUG / Friends ---------------------------> (10985): Service: OnReceive ACTION_HOME_RESUME called 10 -22 09: 35: 06.315: DEBUG / AndroidRuntime (3275): shutdown the virtual machine 10-22 09: 35: 06.315: WARN / dalvikvm (3275): threadid = 1: exit the thread with an 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): on android.view.SurfaceView.reportSurfaceDestroyed (SurfaceView.java : 613) 10-22 09: 35: 06.315: ERROR / AndroidRuntime (3275): on android.view.SurfaceView.updateWindow (SurfaceView.java:498) 10-22 09: 35: 06.315: ERROR / And roidRuntime (3275): at android.view.SurfaceView.updateWindow (SurfaceView.java:407) 10-22 09: 35: 06.315: ERROR / AndroidRuntime (3275): on 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): on android.view.ViewRoot.performTraversals (ViewRoot.java:790) 10-22 09: 35: 06.315: ERROR / AndroidRuntime (3275): on 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: OSH KA / 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): with java.lang.reflect.Method.invokeNative (native method) 10-22 09: 35: 06.315: ERROR / AndroidRuntime (3275): in 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): in dalvik.system.NativeStart.main (native method) 10-22 09: 35: 06.320: ERROR / (18080): Dumpstate> / data / log / dumpstate_app_error

+4
source share
2 answers

I do not think this part is necessary:

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

If you use this, you will need to notify the lock release wait (); But try to use the same code without synchronized (mPauseLock)

EDIT:

The solution to the problem when you press the "Home" button, the application crashes:

 @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 a constructor:

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

My application accidentally (quite often) crashed when returning to a previous activity. It turns out that the "try" operator does not work as expected, since Canvas c may still be zero 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); } //... } 
+3
source

All Articles