Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android自定义视图未显示_Java_Android - Fatal编程技术网

Java Android自定义视图未显示

Java Android自定义视图未显示,java,android,Java,Android,我正在开发Android版的Gomoku。一般来说,我对Java很陌生,对Android更是如此。我正在关注一本名叫《Hello Android》的书,书中作者通过制作数独游戏来教授基础知识。我不严格地遵循它,忽略了我的Gomoku不需要的特性。然而,当新的游戏被按下时,一个新的观点被提出,尽管这本书继续着,好像它应该工作一样,但是应该画的东西对我来说一点也没有显示出来。下面是处理这些内容的代码: Mainactivity.java: private void startGame() {

我正在开发Android版的Gomoku。一般来说,我对Java很陌生,对Android更是如此。我正在关注一本名叫《Hello Android》的书,书中作者通过制作数独游戏来教授基础知识。我不严格地遵循它,忽略了我的Gomoku不需要的特性。然而,当新的游戏被按下时,一个新的观点被提出,尽管这本书继续着,好像它应该工作一样,但是应该画的东西对我来说一点也没有显示出来。下面是处理这些内容的代码:

Mainactivity.java:

private void startGame() {
    Log.d(TAG, "Clicked New Game");
    Intent intent = new Intent(this, Game.class);
    startActivity(intent);
}
Game.java:

public class Game extends Activity {
    private static final String TAG = "Game";
    private int board[] = new int[10 * 10];
    private GameView gameView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "Game.onCreate called");
        gameView = new GameView(this);
        gameView.requestFocus();
        Log.d(TAG, "Game.onCreate finished");
    }
}
GameView.java:

public class GameView extends View {
    private static final String TAG = "Game";
    private float width; //Width of one tile
    private float height; //Height of one tile
    private final Game game;
    Paint background = new Paint();
    Paint dark = new Paint();
    Paint light = new Paint();
    Paint hilite = new Paint();

    public GameView(Context context) {
        super(context);
        this.game = (Game) context;
        setFocusable(true);
        setFocusableInTouchMode(true);
        Log.d(TAG, "GameView finished");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w / 10f;
        height = h /10f;
        Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //Draw the background
        background.setColor(getResources().getColor(R.color.background));
        canvas.drawRect(0, 0, getWidth(), getHeight(), background);
        //Draw the board
        //Define colors for grid lines
        dark.setColor(getResources().getColor(Color.DKGRAY));
        light.setColor(getResources().getColor(Color.LTGRAY));
        hilite.setColor(getResources().getColor(Color.WHITE));
        for (int i = 0; i < 10; i++) {
            Log.d(TAG, "Drawing...");
            canvas.drawLine(0,  i * height - 1,  getWidth(), i * height - 1, light);
            canvas.drawLine(0,  i * width - 1,  getHeight(), i * width - 1, light);
            canvas.drawLine(0,  i * height,  getWidth(), i * height, hilite);
            canvas.drawLine(0,  i * width,  getHeight(), i * width, hilite);
            canvas.drawLine(0,  i * height + 1,  getWidth(), i * height + 1, dark);
            canvas.drawLine(0,  i * width + 1,  getHeight(), i * width + 1, dark);
        }

    }
}
公共类游戏视图扩展视图{
私有静态最终字符串TAG=“Game”;
私有浮动宽度;//一个磁贴的宽度
私有浮动高度;//一个磁贴的高度
私人决赛;
绘制背景=新绘制();
深色油漆=新油漆();
油漆灯=新油漆();
Paint hilite=新的Paint();
公共游戏视图(上下文){
超级(上下文);
this.game=(游戏)上下文;
设置聚焦(真);
setFocusableInTouchMode(真);
Log.d(标记“GameView已完成”);
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(w,h,oldw,oldh);
宽度=w/10f;
高度=h/10f;
Log.d(标签,“onSizeChanged:宽度“+宽度+”,高度“+高度);
}
@凌驾
受保护的void onDraw(画布){
//画背景
setColor(getResources().getColor(R.color.background));
drawRect(0,0,getWidth(),getHeight(),background);
//画板
//定义网格线的颜色
深色.setColor(getResources().getColor(Color.DKGRAY));
setColor(getResources().getColor(Color.LTGRAY));
hilite.setColor(getResources().getColor(Color.WHITE));
对于(int i=0;i<10;i++){
Log.d(标记“图纸…”);
drawLine(0,i*height-1,getWidth(),i*height-1,light);
drawLine(0,i*width-1,getHeight(),i*width-1,light);
drawLine(0,i*height,getWidth(),i*height,hilite);
绘制线(0,i*width,getHeight(),i*width,hilite);
画布.抽绳(0,i*height+1,getWidth(),i*height+1,深色);
画布.抽绳(0,i*width+1,getHeight(),i*width+1,深色);
}
}
}
我试着将作者的代码与我的代码进行比较,除了我没有实现的功能之外,他的代码似乎是匹配的。
但是,Log.d(标记“onSizeChanged:width”+width+,height”+height);未出现在LogCat中,因此我假设此函数从未被调用,我不明白为什么。

您需要在
游戏的
onCreate
中使用
设置内容视图(gameView)
将视图设置为活动

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "Game.onCreate called");
    gameView = new GameView(this);
    setContentView(gameView); // missing
    ...//rest of the code

您只需创建GameView的实例,但不将其添加到活动中。通过使用

setContentView(gameView);
在onCreate方法中