Note that there are some explanatory texts on larger screens.

plurals
  1. POSurfaceView crashes on surfaceChange Android
    text
    copied!<p>I have made an example that extend <code>SurfaceView</code> and <code>Thread</code> to draw some objects on canvas in Android but I'm facing two problems.</p> <ol> <li><p>When press back button the application closed and then show me that "Unfortunately, your example has stopped".</p></li> <li><p>When change the orientation from "Portrait" to "Landscape" or VS , the application failed to adjust the new screen and does not update the drawing then it is crash and show the same message "Unfortunately, your example has stopped".</p></li> </ol> <p>Please review code:</p> <pre><code>@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO implement this method holder.getSurface().setSize(width, height); holder.setFormat(format); } @Override public void surfaceCreated(SurfaceHolder holder) { // at this point the surface is created and // we can safely start the game loop thread.setRunning(true); thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // tell the thread to shut down and wait for it to finish // this is a clean shutdown boolean retry = true; while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { // try again shutting down the thread } } } </code></pre> <p>When I check the logcat I found the following line that the problem in this part</p> <pre><code>@Override public void run() { Canvas canvas; // initialise timing elements for stat gathering initTimingElements(); long beginTime; // the time when the cycle begun long timeDiff; // the time it took for the cycle to execute int sleepTime; // ms to sleep (&lt;0 if we're behind) int framesSkipped; // number of frames being skipped sleepTime = 0; while (running) { canvas = null; // try locking the canvas for exclusive pixel editing // in the surface try { canvas = this.surfaceHolder.lockCanvas(); LoggerHandler.log("canvas : " + canvas); synchronized (surfaceHolder) { beginTime = System.currentTimeMillis(); framesSkipped = 0; // resetting the frames skipped // update game state this.gamePanel.update(); // render state to the screen // draws the canvas on the panel this.gamePanel.render(canvas); // calculate how long did the cycle take timeDiff = System.currentTimeMillis() - beginTime; // calculate sleep time sleepTime = (int) (FRAME_PERIOD - timeDiff); if (sleepTime &gt; 0) { // if sleepTime &gt; 0 we're OK try { // send the thread to sleep for a short period // very useful for battery saving Thread.sleep(sleepTime); } catch (InterruptedException e) { } } while (sleepTime &lt; 0 &amp;&amp; framesSkipped &lt; MAX_FRAME_SKIPS) { // we need to catch up this.gamePanel.update(); // update without rendering sleepTime += FRAME_PERIOD; // add frame period to check // if in next frame framesSkipped++; } // for statistics framesSkippedPerStatCycle += framesSkipped; // calling the routine to store the gathered statistics storeStats(); } } finally { // in case of an exception the surface is not left in // an inconsistent state if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } // end finally } } </code></pre> <p>The problem in this line:</p> <pre><code>canvas = this.surfaceHolder.lockCanvas(); </code></pre> <p>It is return <code>null</code> when change the orientation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload