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 = 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 c.save() method.

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?


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.


  • Getting ticks in figure with subplots
  • Fast rolling correlation in Matlab
  • Android SurfaceView onTouchEvent not getting called
  • Correlation matrix heatmap with multiple datasets that have matching columns
  • Android SurfaceView not retaining previously drawn objects
  • How to calculate correlation between all columns and remove highly correlated ones using python or p
  • Live Wallpaper + bitmap + canvas
  • How to draw a bitmap to a SurfaceView?
  • Texture View video and Bitmap display
  • How to set UIImageView in the center of screen horizontally or vertically with Swift, but no more St
  • How to terminate a process
  • How to check h2 database health and corruption
  • What types of languages allow programmatic creation of variable names?
  • Can I make a variable temporarily volatile?
  • Restoring deleted mysql database
  • Force a controller to always act as a proxy to a model in Ember
  • What could cause numpy.nanstd() to return nan?
  • Connection String in a WIX Custom Action Parameter
  • Strange multiple assignment error in C++
  • SoundPool is loading sounds but every time I play it it says “sample ___ is not ready”?
  • How to resend request with android volley when not authorized
  • Removing event listeners on automatically created multiple elements
  • Java making confirming exit
  • Open Existing DB in MySQL WorkBench
  • C#: Import/Export Settings into/from a File
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • Memory error in python- how to use more memory
  • How to handle images sent by a mobile device?
  • Recording logins for password protected directories
  • Pass value from viewmodel to script in zk
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Nant, Vault & Windows Integrated Authentication
  • What is Eclipse's Declaration View used for?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Perl system calls when running as another user using sudo
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How can I estimate amount of memory left with calling System.gc()?
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • Matrix multiplication with MKL
  • Benchmarking RAM performance - UWP and C#
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Append folder name and increment by 1 using batch script
  • Converting MP3 duration time