NullPointerException:尝试调用虚方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)

时间:2016-03-05 22:40:02

标签: java android xml

我得到一个nullpointer异常,并且不知道为什么。这是代码:

这是一个Tic Tac Toe游戏,但当我尝试为我的游戏上的按钮设置onClickListener时会抛出异常。

Mainactivity:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int board[][];

    // button map
    Button buttons[][];


    int i, j;


    TextView textView;


    AI ai;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private boolean checkBoard() {

        // is the game over?
        boolean gameOver = false;

        // first check all possible combinations to see if the user has won.
        if ((board[1][1] == 0 && board[2][2] == 0 && board[3][3] == 0)
                || (board[1][3] == 0 && board[2][2] == 0 && board[3][1] == 0)
                || (board[1][2] == 0 && board[2][2] == 0 && board[3][2] == 0)
                || (board[1][3] == 0 && board[2][3] == 0 && board[3][3] == 0)
                || (board[1][1] == 0 && board[1][2] == 0 && board[1][3] == 0)
                || (board[2][1] == 0 && board[2][2] == 0 && board[2][3] == 0)
                || (board[3][1] == 0 && board[3][2] == 0 && board[3][3] == 0)
                || (board[1][1] == 0 && board[2][1] == 0 && board[3][1] == 0)) {

            // user has won
            textView.setText("You win!");
            gameOver = true;

        } else if ((board[1][1] == 1 && board[2][2] == 1 && board[3][3] == 1)
                || (board[1][3] == 1 && board[2][2] == 1 && board[3][1] == 1)
                || (board[1][2] == 1 && board[2][2] == 1 && board[3][2] == 1)
                || (board[1][3] == 1 && board[2][3] == 1 && board[3][3] == 1)
                || (board[1][1] == 1 && board[1][2] == 1 && board[1][3] == 1)
                || (board[2][1] == 1 && board[2][2] == 1 && board[2][3] == 1)
                || (board[3][1] == 1 && board[3][2] == 1 && board[3][3] == 1)
                || (board[1][1] == 1 && board[2][1] == 1 && board[3][1] == 1)) {

            // computer has won
            textView.setText("You lost!");
            gameOver = true;

        }
        // Nobody has won but we still need to make sure that we have empty spaces
        else {
            boolean isEmpty = true;
            for(i=1; i<=3; i++) {
                for(j=1; j<=3; j++) {
                    if(board[i][j] == 2) {
                        isEmpty = false;
                        break;
                    }
                }
            }

            if(isEmpty) {
                gameOver = true;
                textView.setText("It's a draw!");
            }
        }

        return gameOver;
    }

    private class AI {
        // the computer checks the board and takes its turn
        public void takeTurn() {
            try {
                if (board[1][1] == 2 &&
                        ((board[1][2] == 0 && board[1][3] == 0) ||
                                (board[2][2] == 0 && board[3][3] == 0) ||
                                (board[2][1] == 0 && board[3][1] == 0))) {
                    markSquare(1, 1);
                } else if (board[1][2] == 2 &&
                        ((board[2][2] == 0 && board[3][2] == 0) ||
                                (board[1][1] == 0 && board[1][3] == 0))) {
                    markSquare(1, 2);
                } else if (board[1][3] == 2 &&
                        ((board[1][1] == 0 && board[1][2] == 0) ||
                                (board[3][1] == 0 && board[2][2] == 0) ||
                                (board[2][3] == 0 && board[3][3] == 0))) {
                    markSquare(1, 3);
                } else if (board[2][1] == 2 &&
                        ((board[2][2] == 0 && board[2][3] == 0) ||
                                (board[1][1] == 0 && board[3][1] == 0))) {
                    markSquare(2, 1);
                } else if (board[2][2] == 2 &&
                        ((board[1][1] == 0 && board[3][3] == 0) ||
                                (board[1][2] == 0 && board[3][2] == 0) ||
                                (board[3][1] == 0 && board[1][3] == 0) ||
                                (board[2][1] == 0 && board[2][3] == 0))) {
                    markSquare(2, 2);
                } else if (board[2][3] == 2 &&
                        ((board[2][1] == 0 && board[2][2] == 0) ||
                                (board[1][3] == 0 && board[3][3] == 0))) {
                    markSquare(2, 3);
                } else if (board[3][1] == 2 &&
                        ((board[1][1] == 0 && board[2][1] == 0) ||
                                (board[3][2] == 0 && board[3][3] == 0) ||
                                (board[2][2] == 0 && board[1][3] == 0))) {
                    markSquare(3, 1);
                } else if (board[3][2] == 2 &&
                        ((board[1][2] == 0 && board[2][2] == 0) ||
                                (board[3][1] == 0 && board[3][3] == 0))) {
                    markSquare(3, 2);
                } else if (board[3][3] == 2 &&
                        ((board[1][1] == 0 && board[2][2] == 0) ||
                                (board[1][3] == 0 && board[2][3] == 0) ||
                                (board[3][1] == 0 && board[3][2] == 0))) {
                    markSquare(3, 3);
                }
                // There is nothing to block so choose a random square
                else {
                    Random rand = new Random();
                    int a = rand.nextInt(4);
                    int b = rand.nextInt(4);
                    while (a == 0 || b == 0 || board[a][b] != 2) {
                        a = rand.nextInt(4);
                        b = rand.nextInt(4);
                    }
                    markSquare(a, b);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        // Mark the selected square
        private void markSquare(int x, int y) {
            buttons[x][y].setEnabled(false);
            buttons[x][y].setText("X");
            board[x][y] = 1;
            checkBoard();
        }
    }
    class ClickListener implements View.OnClickListener {
        // this buttons position in the arrays
        int x;
        int y;

        public ClickListener(int x, int y) {
            this.x = x;
            this.y = y;
        }

        // handle the click event
        public void onClick(View view) {
            // check to see if the button is enabled
            if(buttons[x][y].isEnabled()) {
                buttons[x][y].setEnabled(false);
                // mark it as belonging to the user
                buttons[x][y].setText("O");
                board[x][y] = 0;

                // check to see if the user has won, if not let AI take turn
                if(!checkBoard()) {
                    ai.takeTurn();
                }
            }
        }
    }
    private void init() {
        ai = new AI();
        buttons = new Button[4][4];
        board = new int[4][4];

        // get the objects defined in main.xml
        textView = (TextView) findViewById(R.id.dialogue);

        buttons[1][3] = (Button) findViewById(R.id.one);
        buttons[1][2] = (Button) findViewById(R.id.two);
        buttons[1][1] = (Button) findViewById(R.id.three);
        buttons[2][3] = (Button) findViewById(R.id.four);
        buttons[2][2] = (Button) findViewById(R.id.five);
        buttons[2][1] = (Button) findViewById(R.id.six);
        buttons[3][3] = (Button) findViewById(R.id.seven);
        buttons[3][2] = (Button) findViewById(R.id.eight);
        buttons[3][1] = (Button) findViewById(R.id.nine);

        // set the values of the board to 2 (empty)
        for (i = 1; i <= 3; i++) {
            for (j = 1; j <= 3; j++)
                board[i][j] = 2;
        }

        // add the click listeners for each button
        for (i = 1; i <= 3; i++) {
            for (j = 1; j <= 3; j++) {
                buttons[i][j].setOnClickListener(new ClickListener(i, j));
                if(!buttons[i][j].isEnabled()) {
                    buttons[i][j].setText(" ");
                    buttons[i][j].setEnabled(true);
                }
            }
        }
    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="azval.tictactoe.MainActivity">

    <Button android:id="@+id/one"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_alignParentTop="true" android:layout_toRightOf="@+id/two" android:layout_toEndOf="@+id/two"/>

    <Button android:id="@+id/two"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_alignParentTop="true" android:layout_toRightOf="@+id/three"
        android:layout_toEndOf="@+id/three"/>

    <Button android:id="@+id/three"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:padding="0px"
        android:layout_alignTop="@+id/two"
        android:layout_alignStart="@+id/six" />

    <Button android:id="@+id/four"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_below="@+id/one" android:layout_toRightOf="@+id/two"/>

    <Button android:id="@+id/five"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_below="@+id/two" android:layout_toRightOf="@+id/three"/>

    <Button android:id="@+id/six"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_below="@+id/three"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="61dp" />

    <Button android:id="@+id/seven"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_below="@+id/four" android:layout_toRightOf="@+id/two"/>

    <Button android:id="@+id/eight"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_below="@+id/five" android:layout_toRightOf="@+id/three"/>

    <Button android:id="@+id/nine"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text=" "
        android:textSize="70px"
        android:layout_below="@+id/six"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="61dp" />

    <TextView android:id="@+id/dialogue"
        android:layout_width="fill_parent"
        android:layout_below="@id/nine"
        android:layout_height="wrap_content"
        android:text="Click a button to start"
        android:gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textSize="15sp"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在活动的视图层次结构中找到视图之前,您实际上需要将一些内容传递给活动。在setContentView(R.layout.your_activity_layout)之前致电init()