Android java.lang.IllegalArgumentException

六月ゝ 毕业季﹏ 提交于 2020-01-14 10:34:27

问题


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.


回答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.




回答2:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/9792446/android-java-lang-illegalargumentexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!