Canvas 对象必须与之前由 lockCanvas 返回的实例相同
Canvas object must be the same instance that was previously returned by lockCanvas
我有一个自定义的 SurfaceView,它带有绘制背景和位图的方法 doDraw()。问题是当我 运行 我得到一个错误
Caused by: java.lang.IllegalArgumentException: canvas object must be the same instance that was previously returned by lockCanvas
我不明白为什么会这样。我没有在我的代码中的任何其他地方声明任何其他画布。我只有另外两个 classes,MainActivity 和 SurfaceViewExample。 MainActivity 只是有一个打开 SurfaceViewExample 的意图,而 SurfaceViewExample 只是有一个被一些按钮调用的方法。
我们的观点class:
package com.thatoneprogrammerkid.gameminimum;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class OurView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Bitmap testimg;
public int xCoord = 500;
public int yCoord = 500;
public OurView(Context context) {
super(context);
init();
}
public OurView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OurView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
holder = getHolder();
holder.addCallback(this);
testimg = BitmapFactory.decodeResource(getResources(),R.drawable.testimg);
}
void moveImage(int xChange, int yChange) {
xCoord += xChange;
yCoord += yChange;
doDraw();
}
void doDraw() {
Canvas myCanvas = holder.lockCanvas();
if (myCanvas != null) {
myCanvas.drawARGB(255, 55, 255, 255);
myCanvas.drawBitmap(testimg, xCoord, yCoord, null);
}
holder.unlockCanvasAndPost(myCanvas);
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
doDraw();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
将 unlockCanvasAndPost()
移动到 if (myCanvas != null) {
语句中。我的猜测是 lockCanvas()
返回 null,而您正在尝试解锁 null 引用。
查看 the source code,"is it the same" 的测试先于 "is it locked at all" 的测试——并且 mCanvas
被初始化为非空值。
我有一个自定义的 SurfaceView,它带有绘制背景和位图的方法 doDraw()。问题是当我 运行 我得到一个错误
Caused by: java.lang.IllegalArgumentException: canvas object must be the same instance that was previously returned by lockCanvas
我不明白为什么会这样。我没有在我的代码中的任何其他地方声明任何其他画布。我只有另外两个 classes,MainActivity 和 SurfaceViewExample。 MainActivity 只是有一个打开 SurfaceViewExample 的意图,而 SurfaceViewExample 只是有一个被一些按钮调用的方法。
我们的观点class:
package com.thatoneprogrammerkid.gameminimum;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class OurView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Bitmap testimg;
public int xCoord = 500;
public int yCoord = 500;
public OurView(Context context) {
super(context);
init();
}
public OurView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OurView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
holder = getHolder();
holder.addCallback(this);
testimg = BitmapFactory.decodeResource(getResources(),R.drawable.testimg);
}
void moveImage(int xChange, int yChange) {
xCoord += xChange;
yCoord += yChange;
doDraw();
}
void doDraw() {
Canvas myCanvas = holder.lockCanvas();
if (myCanvas != null) {
myCanvas.drawARGB(255, 55, 255, 255);
myCanvas.drawBitmap(testimg, xCoord, yCoord, null);
}
holder.unlockCanvasAndPost(myCanvas);
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
doDraw();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
将 unlockCanvasAndPost()
移动到 if (myCanvas != null) {
语句中。我的猜测是 lockCanvas()
返回 null,而您正在尝试解锁 null 引用。
查看 the source code,"is it the same" 的测试先于 "is it locked at all" 的测试——并且 mCanvas
被初始化为非空值。