I am trying to follow the tutorial here:
http://www.edu4java.com/en/androidgame/androidgame2.html
But fell into a trap. For some reason, eclipse does not recognize getHolder () and gives me the following error:
The getHolder () method is undefined for the GameView type
And the following solutions:
Change to getHandler (...); Create method getHolder ()
The code is almost the same as in the tutorial, but here is what I mean:
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); } }
source share