gpt4 book ai didi

Java:安卓开发: "The method getHolder() is undefined for the type GameView"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:51:16 26 4
gpt4 key购买 nike

我一直在尝试按照此处的教程进行操作:

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 {

关于Java:安卓开发: "The method getHolder() is undefined for the type GameView",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12830979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com