I’ve been trying to follow the tutorial here:
http://www.edu4java.com/en/androidgame/androidgame2.html
But have hit a snag. For some reason, eclipse doesn’t recognise getHolder() and gives me the following error:
The method getHolder() is undefined for the type GameView
And the following solutions:
Change to getHandler(…);
Create method getHolder()
The code is pretty much the same as the tutorial, but here is what I have anyway:
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()is a method ofSurfaceView, and you’re extendingView. Change to: