我正在开发动态壁纸,但我遇到了一个困扰我一段时间的问题。我的动态壁纸在变成横向然后连续几次回到纵向时崩溃。LogCat 显示以下错误:
致命异常:在 com.android.internal.view.BaseSurfaceHolder.unlockCanvasAndPost(BaseSurfaceHolder.java:215) 在 livewallpaper.LiveWallpaper$CatWallEngine.draw( LiveWallpaper.java:167)....
这是代码:
public void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
paint = new Paint();
try {
c = holder.lockCanvas();
if (c != null ) {
//drawing code goes here
}
} finally {
if (c != null) {
holder.unlockCanvasAndPost(c);
}
}
mHandler.removeCallbacks(mUpdateDisplay);
if (mVisible) {
mHandler.postDelayed(mUpdateDisplay, 50);
}
第 167 行是 holder.unlockCanvasAndPost(c); 线。我在其他地方读到过,试图解锁未锁定的画布可能会导致此错误,但我已经检查过了,这不是问题:每次尝试解锁之前,画布都会被锁定。