我正在用SurfaceView和线程制作游戏。在主线程中,我从SurfaceHolder锁定了画布,然后解锁了画布并发布了锁定它时获得的同一画布。当我使用简单的意图重新启动游戏时,出现以下错误消息:
java.lang.IllegalArgumentException: canvas object must be the same instance that was previously returned by lockCanvas
at android.view.Surface.unlockSwCanvasAndPost(Surface.java:370)
at android.view.Surface.unlockCanvasAndPost(Surface.java:363)
at android.view.SurfaceView$3.unlockCanvasAndPost(SurfaceView.java:1089)
at com.my.app.MainThread.run(MainThread.java:61)
这是我的代码:
Log.d(TAG, "Locking canvas!");
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.gameView.update(millitime);
if(canvas != null)
this.gameView.draw(canvas);
}
if(canvas != null) {
Log.d(TAG, "Unlocking canvas!");
this.surfaceHolder.unlockCanvasAndPost(canvas);
} else {
Log.d(TAG, "Canvas is null!");
}
出什么问题了?我确实返回了相同的画布,但是已对其进行了编辑。所以那不是问题吧?谢谢!
顺便说一句,我看到了这篇文章:Canvas object must be the same instance that was previously returned by lockCanvas,但在那个问题上,SurfaceHolder.unlockAndPost在if(canvas!= null)之外。