Skip to content Skip to sidebar Skip to footer

Surfaceholder.lockcanvas Returns Null If The Surface Is Not In The Foreground

I'm currently doing a test with a LiveWallpaper in Android. I am drawing something on the canvas using code that looks something like this: final SurfaceHolder holder = getSurfaceH

Solution 1:

That's right. A common way to avoid this is to unregister your listener in onPause or onVisibilityChanged(false), and reregister in onResume or onVisibilityChanged(true), since you shouldn't react to settings changes when your canvas isn't visible.

Another solution would be to simply surround that section of code with a null check, and forget about it. I'd recommend against this, though, since what you really want to do is prevent your code from even attempting to draw to the surface when it's not in view.

Solution 2:

I realize that this question is ancient, but having had the same question and having found a different answer that worked for me, I thought I'd share for posterity.

Check if SurfaceHolder.getSurface.isValid() before getting the canvas from the surface. Fixed my null canvas issues.

Post a Comment for "Surfaceholder.lockcanvas Returns Null If The Surface Is Not In The Foreground"