Android java.lang.IllegalArgumentException

2019-06-25 05:47发布

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.

2条回答
手持菜刀,她持情操
2楼-- · 2019-06-25 06:26

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-25 06:35

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;
        }
    }
}
查看更多
登录 后发表回答