java.lang.IllegalArgumentException: canvas object must be the same instance that was previously r...

http://blog.sina.com.cn/s/blog_90d4a0e70102wajh.html

4、SurfaceView和Canvas

java.lang.IllegalArgumentException: canvas object must be the same instance that was previously returned by lockCanvas

android.view.Surface.unlockCanvasAndPost

android.view.SurfaceView$4.unlockCanvasAndPost

......

​在渲染SurfaceView时,我们常常这么写:

final Canvas canvas = holder.lockCanvas();​

//TO DO something

holder.unlockCanvasAndPost(canvas);​

看起来这个canvas不可能改变,然而还是会出现上面的异常。常见的是正在渲染时,按下home键退到主页。

解决方法很简单,在SurfaceView内新建一个flag,标记surface是否被销毁

SurfaceHolder 设置Callback

在holder.lockCanvas()前和holder.unlockCanvasAndPost(canvas)前判断这个flag,为true直接return。

你可能感兴趣的:(java.lang.IllegalArgumentException: canvas object must be the same instance that was previously r...)