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 = getSurfaceHolder(); Canvas c = new Canvas(); c = holder.lockCanvas(); // c becomes null c.save(); c.drawBitmap(currentBitmap); c.restore(); holder.unlockCanvasAndPost(c);
<em>This part is working fine under normal circumstances</em>. However, I have a listener that executes this code whenever a setting is changed in the Settings that correspond to this service. It seems that whenever I execute this code from the settings activity, I am getting a
NullPointer on the
It seems that only when the Wallpaper is not in the foreground, the holder.lockCanvas(). Is it impossible to draw to this surface when it's not in the foreground?Answer1:
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.