java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView [重复]

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {     私人Button [] []按钮= new Button [3] [3];     private boolean player1Turn = true;

private int roundCount;

private int player1Points;
private int player2Points;

private TextView text1;
private TextView text2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView text1 = findViewById(R.id.t1);
    TextView text2 = findViewById(R.id.t2);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            String buttonID = "button_" + i + j;
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            buttons[i][j] = findViewById(resID);
            buttons[i][j].setOnClickListener(this);


        }
    }
    Button reset = findViewById(R.id.reset);
    reset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

@Override
public void onClick(View v) {
    if (!((Button) v).getText().toString().equals("")) {
        return;
    }
    if (player1Turn) {
        ((Button) v).setText("X");
    } else {
        ((Button) v).setText("O");
    }
    roundCount++;
    if (checkForWin()) {
        if (player1Turn) {
            player1Wins();
        } else {
            player2Wins();

        }
    } else if (roundCount == 9) {
        draw();
    } else {
        player1Turn = !player1Turn;
    }


}

private boolean checkForWin() {
    String[][] field = new String[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            field[i][j] = buttons[i][j].getText().toString();

        }
    }
    for (int i = 0; i < 3; i++) {
        if (field[i][0].equals(field[i][1])
                && field[i][0].equals(field[i][2])
                && !field[i][0].equals("")) {
            return true;
        }
    }
    for (int i = 0; i < 3; i++) {
        if (field[0][i].equals(field[1][i])
                && field[0][i].equals(field[2][i])
                && !field[0][i].equals("")) {
            return true;
        }
    }
    if (field[0][0].equals(field[1][1])
            && field[0][0].equals(field[2][2])
            && !field[0][0].equals("")) {
        return true;
    }
    if (field[0][2].equals(field[1][1])
            && field[0][2].equals(field[2][0])
            && !field[0][2].equals("")) {
        return true;
    }
    return false;

}

private void player1Wins() {
    player1Points++;
    Toast.makeText(this, "Player 1 Wins!!", Toast.LENGTH_SHORT).show();
    updatePointsText();
    resetBoard();
}

private void player2Wins() {
    player2Points++;
    Toast.makeText(this, "Player 2 Wins!!", Toast.LENGTH_SHORT).show();
    updatePointsText();
    resetBoard();
}

private void draw() {
    Toast.makeText(this, "Draw!!", Toast.LENGTH_SHORT).show();
    resetBoard();
}
@SuppressLint("DefaultLocale")
private void updatePointsText(){
    text1.setText("Player 1: "+player1Points);
    text2.setText("Player 2: "+player2Points);
}
private void resetBoard(){
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            buttons[i][j].setText("");
        }
    }
    roundCount =0;
    player1Turn =true;
}

}

---------崩溃开始 E / AndroidRuntime:致命异常:main     流程:com.example.tictaktoe,PID:3016     java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TextView.setText(java.lang.CharSequence)”         在com.example.tictaktoe.MainActivity.updatePointsText(MainActivity.java:133)         在com.example.tictaktoe.MainActivity.player1Wins(MainActivity.java:116)         在com.example.tictaktoe.MainActivity.onClick(MainActivity.java:63)         在android.view.View.performClick(View.java:4780)         在android.view.View $ PerformClick.run(View.java:19866)         在android.os.Handler.handleCallback(Handler.java:739)         在android.os.Handler.dispatchMessage(Handler.java:95)         在android.os.Looper.loop(Looper.java:135)         在android.app.ActivityThread.main(ActivityThread.java:5254)         在java.lang.reflect.Method.invoke(本机方法)         在java.lang.reflect.Method.invoke(Method.java:372)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

评论
A书白
A书白

是否可以在onCreate中用类变量而不是局部变量替换text1和text2。在类的开头,已经将text1和text2作为类变量给出。但是这些值已分配给局部变量。

如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      text1 = findViewById(R.id.t1);
      text2 = findViewById(R.id.t2);
    ...
 }
点赞
评论