Android avatar image
Android asked

IllegalArgumentException on lockCanvas()

We have a bug in which our app crashes (android:screenOrientation="portrait"), when the following takes place: - User presses the sleep button once. - User presses the sleep button again. - Device (showing the 'locked' screen) is turned to landscape position. I do not understand why an exception is thrown. I have listed the stack trace and also the code from our main loop. How can we tweak the main loop so that this doesn't happen? Any help would be greatly appreciated! This exception is thrown: E/SurfaceTextureClient(9160): dequeueBuffer failed (Bad file number) E/SurfaceHolder(9160): Exception locking surface E/SurfaceHolder(9160): java.lang.IllegalArgumentException E/SurfaceHolder(9160): at android.view.Surface.lockCanvasNative(Native Method) E/SurfaceHolder(9160): at android.view.Surface.lockCanvas( E/SurfaceHolder(9160): at android.view.SurfaceView$4.internalLockCanvas( E/SurfaceHolder(9160): at android.view.SurfaceView$4.lockCanvas( E/SurfaceHolder(9160): at E/SurfaceHolder(9160): at And our main loops looks like this: public void run() { running = true; while (running) { Canvas canvas = null; try { canvas = surface.lockCanvas(null); synchronized (surface) { // game logic if (canvas!=null) { try { paint(canvas); } catch (Exception e) { } } } } catch (Exception e) { } finally { if (canvas!=null) try { surface.unlockCanvasAndPost(canvas); } catch (Exception e) {} } } }
fire tablet
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Jonathan, Thank you for writing to us and sorry for the late response. You should unlock the canvas after drawing on it. The right sequence should be: 1. Get the canvas instance by calling mSurfaceHolder.lockCanvas(); 2. Draw on the canvas. 3. Unlock canvas calling mSurfaceHolder.unlockCanvasAndPost(c); The code should be, public boolean onTouch(View v, MotionEvent me) { Canvas c = mSurfaceHolder.lockCanvas(); drawTap(c, v); mSurfaceHolder.unlockCanvasAndPost(c); return true; } Hope this helps.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.