1
我是一个尝试将android井字游戏应用程序放在一起的java新手。 我有下面的代码,但不断收到有关我在监听器类中使用变量的错误。我的直觉是它可能是一个范围问题,但我不知道如何解决它。下面是完整的代码:mBoardButtons无法解析为变量
------------snip---------------------
package edu.harding.tictactoe;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidTicTacToeActivity extends Activity {
// Represents the internal state of the game
//a variable to make the game logic available in the Activity class - Elton.
private TicTacToeGame mGame;
// Buttons making up the board - Elton
private Button mBoardButtons[];
// Various text displayed - Elton
private TextView mInfoTextView;
public int BOARD_SIZE = 8;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_tic_tac_toe);
mBoardButtons = new Button[BOARD_SIZE]; //cheat by creating a local variable BOARD_SIZE.
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);
mInfoTextView = (TextView) findViewById(R.id.information);
mGame = new TicTacToeGame();
startNewGame(); //start a new game - Elton.
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_tic_tac_toe, menu);
return true;
}
//a method to start anew game - Elton
// Set up the game board.
private void startNewGame() {
mGame.clearBoard(); //clear the board - Elton
// Reset all buttons
for (int i = 0; i < mBoardButtons.length; i++) {
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
// Human goes first
mInfoTextView.setText("You go first.");
}
private void setMove(char player, int location) {
mGame.setMove(player, location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == TicTacToeGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.rgb(0, 200, 0));
else
mBoardButtons[location].setTextColor(Color.rgb(200, 0, 0));
}
}
//Handles clicks on the game board buttons
class ButtonClickListener implements View.OnClickListener {
int location;
public ButtonClickListener(int location) { //this is a constructor.
this.location = location;
}
public void onClick(View view) {
if (mBoardButtons[location].isEnabled()) {
setMove(TicTacToeGame.HUMAN_PLAYER, location);
// If no winner yet, let the computer make a move
int winner = mGame.checkForWinner();
if (winner == 0) {
mInfoTextView.setText("It's Android's turn.");
int move = mGame.getComputerMove();
setMove(TicTacToeGame.COMPUTER_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText("It's your turn.");
else if (winner == 1)
mInfoTextView.setText("It's a tie!");
else if (winner == 2)
mInfoTextView.setText("You won!");
else
mInfoTextView.setText("Android won!");
}
}
}
-----------------snap----------------------------------------
The errors I get are:
mBoardButtons cannot be resolved to a variable
The method setMove(char, int) is undefined for the type ButtonClickListener
mGame cannot be resolved
mInfoTextView cannot be resolved
The method setMove(char, int) is undefined for the type ButtonClickListener
All the above errors appear in the onClick() method of the listener.
Please help.
谢谢您的回复。我如何给它访问?我认为问题的确是我不确定ButtonClickListener类。为什么java中的监听器被声明为类中的类。我是否以正确的方式去做? – Elton
这不是强制性的创建一个单独的类,但你可以定义它“内联”在这里的例子:http://developer.android.com/reference/android/widget/Button.html。然后,您可以将游戏逻辑的代码放入方法中,并在onClick中调用它。 – fasteque