最近在做一个游戏,用到了Surfaceview,程序执行时切换到后台,然后再重新进入会报异常闪退,我在这里总结一下解决办法.
SurfaceView可能报的异常主要有两点:
(一)、提交画布异常。
public void draw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp, bmp_x, bmp_y, paint);
}
} catch (Exception e) {
Log.v("Himi", "draw is Error!");
} finally {//备注1
if (canvas != null)//备注2
sfh.unlockCanvasAndPost(canvas);
}
}