2013-11-21 127 views
0

我正在使用Gomoku的Android版本。对于Java来说,我是一个新手,对于Android来说更是如此。我正在关注一本名为“Hello Android”的书,作者通过制作Sudoku游戏来教授基本知识。我在松散地跟踪它,忽略了我的Gomoku不需要的功能。然而,当新游戏被按下时,一个新的观点被提出,尽管这本书继续下去,好像它应该有效,但是应该绘制的东西根本不会显示出来。下面是与东西打交道的代码:Android自定义视图不显示

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); 
     } 

    } 
} 

我曾尝试将作者的代码与我的代码进行比较,除非我没有实现功能,否则代码似乎是匹配的。但是,Log.d(TAG,“onSizeChanged:width”+ width +“,height”+ height);不会出现在LogCat中,所以我认为这个函数根本就不会被调用,我不明白为什么。

回答

0

您只是创建了一个GameView的实例,但不会将其添加到您的活动中。通过在您的onCreate方法中使用

setContentView(gameView); 

0

您需要将视图设置为setContentView(gameView)的活动onCreateGame活动。

@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