我一直在尝试按照这里的教程:
http://www.edu4java.com/en/androidgame/androidgame2.html
但是遇到了障碍。由于某种原因,eclipse无法识别getHolder()并给我以下错误:
未为类型GameView定义方法getHolder()
以及以下解决方案:
更改为getHandler(...);
创建方法getHolder()
该代码与本教程几乎相同,但是无论如何,这就是我所拥有的:
package com.example.killthemall;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
public class GameView extends View {
private Bitmap bmp;
private SurfaceHolder holder;
public GameView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = holder.lockCanvas(null);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
最佳答案
getHolder()
是SurfaceView
的一种方法,您正在扩展View
。改成:
public class GameView extends SurfaceView {