【发布时间】:2015-05-02 08:38:44
【问题描述】:
您好,我在 Android Studio 中制作了一款游戏。有时,当我在模拟器上关闭并重新启动应用程序时,我会在 logcat 中收到以下垃圾邮件:
Exception locking surface
java.lang.IllegalStateException: Surface was already locked
我尝试用 try-catch 块包围canvas = this.surfaceHolder.lockCanvas();,但没有捕获到异常。有没有办法在尝试锁定之前检查画布是否已锁定?
这是我在 MainThread.java 类中的代码
while (running) {
startTime = System.nanoTime();
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
this.gamePanel.update();
// draws the canvas on the panel
this.gamePanel.draw(canvas);
}
} catch(Exception e){e.printStackTrace();}finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
try {
surfaceHolder.unlockCanvasAndPost(canvas);
}
catch(Exception e){e.printStackTrace();}
}
}
【问题讨论】:
-
你找到答案了吗?
标签: java android exception canvas android-studio