4

This is probably a simple one to answer but I can't seem to get right and thought I would ask. I am getting a java.lang.IllegalArgumentException and a java.lang.NullPointerException

Here is the error log

 03-20 13:13:22.872: E/SurfaceTextureClient(565): dequeueBuffer failed (No such device)
 03-20 13:13:22.879: E/BaseSurfaceHolder(565): Exception locking surface
 03-20 13:13:22.879: E/BaseSurfaceHolder(565): java.lang.IllegalArgumentException
 03-20 13:13:22.879: E/BaseSurfaceHolder(565): at android.view.Surface.lockCanvasNative(Native Method)
 03-20 13:13:22.879: E/BaseSurfaceHolder(565):  at android.view.Surface.lockCanvas(Surface.java:76)
 03-20 13:13:22.879: E/BaseSurfaceHolder(565):  at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:184)
 03-20 13:13:22.879: E/BaseSurfaceHolder(565):  at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:161)
 03-20 13:13:22.879: E/BaseSurfaceHolder(565):  at ca.watercity.CityActivity$Blimp.render(CityActivity.java:235)
 03-20 13:13:22.879: E/BaseSurfaceHolder(565):  at ca.city.CityActivity$CityThread.run(CityActivity.java:580)
 03-20 13:13:22.879: W/dalvikvm(565): threadid=11: thread exiting with uncaught exception (group=0x409c01f8)
 03-20 13:13:22.889: E/AndroidRuntime(565): FATAL EXCEPTION: Thread-79
 03-20 13:13:22.889: E/AndroidRuntime(565): java.lang.NullPointerException
 03-20 13:13:22.889: E/AndroidRuntime(565):     at ca.city.CityActivity$Blimp.render(CityActivity.java:237)
 03-20 13:13:22.889: E/AndroidRuntime(565):     at ca.city.CityActivity$CityThread.run(CityActivity.java:580)


 03-20 13:26:12.633: E/AndroidRuntime(564): java.lang.NullPointerException

Here is the two lines of code it's effecting.

 public void render(){
            Canvas canvas = null;
            try{
                // line 235
                canvas = this._surfaceHolder.lockCanvas(null);
                synchronized (this._surfaceHolder) {
                    canvas.save();
                    this.onDraw(canvas);
                    canvas.restore();
                }
            }finally{
                if(canvas != null){
                    this._surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   
        }

and this these lines of code.

  @Override
        public void run() {
            while(this._running){
                this._blimp.render();
            }
        } // line 580

Any help in this would be greatly appreciated. Thank you in advance.

user870286
  • 67
  • 2
  • 12

2 Answers2

1

You're passing null to SurfaceHolder.lockCanvas. You want to use the no argument version of lockCanvas() instead, assuming you don't have a rect that you want to be treated as dirty.

jjm
  • 6,028
  • 2
  • 24
  • 27
  • http://developer.android.com/reference/android/view/SurfaceHolder.html#lockCanvas%28%29 – jjm Mar 20 '12 at 18:13
  • thanks that took the null exception away but I still have a java.lang.IllegalArgumentException error on the same two lines of code. Not sure what is causing that error, I should of mentioned this only happens in android 4 and up anything below is fine and causes no errors just android 4 and up, maybe it has something to do with that version. – user870286 Mar 20 '12 at 18:57
  • ya its a weird one, thanks for getting rid of half the issue I appreciate it, I'll keep digging away at it and post if I find an answer unless someone else has one for this issue. – user870286 Mar 20 '12 at 19:28
  • By the way the error now happens at unlockCanvasAndPost(canvas); for the java.lang.IllegalArgumentException error. Thought I would add that. – user870286 Mar 20 '12 at 19:34
  • 2
    This happens when a Canvas is locked and locked again (without unlocking it). This situation could happen in your code if render() is called again and you haven't had the chance to unlock the previous canvas – EyalBellisha Apr 04 '13 at 12:14
0

Save in a boolean whether your Canvas instance is already locked or not in order to avoid the execution of the unockCanvasAndPost() method before your Canvas is unlocked from a previous lockCanvas() call:

private boolean canvasLocked;

public void render() {
    Canvas canvas = null;
    try {
        // line 235
        if (!canvasLocked) {
            canvas = this._surfaceHolder.lockCanvas(null);
            canvasLocked = true;
            synchronized (this._surfaceHolder) {
                canvas.save();
                this.onDraw(canvas);
                canvas.restore();
            }
        }
    } finally {
        if (canvas != null) {
            this._surfaceHolder.unlockCanvasAndPost(canvas);
            canvasLocked = false;
        }
    }
}
AxeEffect
  • 6,345
  • 4
  • 37
  • 33