You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android井字棋游戏出现InvocationTargetException错误求助

Android井字棋游戏空指针错误修复

错误原因定位

LogCat显示的NullPointerException出现在MainActivity.java第57行,根源有两点:

  • 全局变量b1-b9仅在count>4时才会被赋值,前4次点击按钮时这些变量仍为null,直接调用equals()方法触发空指针
  • 胜负判断中使用!b1.equals(" "),但按钮初始文本是空字符串""而非空格,逻辑不匹配

修复步骤

  1. 限制胜负判断时机:将胜负判断逻辑移入count>4分支,只有落子数足够形成胜负(至少5步)时才执行判断,避免访问未初始化的变量
  2. 修正空文本判断逻辑:将!b1.equals(" ")改为!b1.isEmpty(),匹配按钮初始空文本状态
  3. 重构newGame方法:原方法直接关闭Activity,改为重置所有按钮文本、计数器和玩家标记,实现真正的重新开始功能
  4. 修复胜负判断逻辑错误:修正原代码中斜线判断的变量错误(b4.equals(b9)改为b6.equals(b9))

修改后的完整代码

MainActivity.java

package com.example.tictactoe;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnNewgame;
    int flag=0;
    int count=0;
    String b1,b2,b3,b4,b5,b6,b7,b8,b9;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        btnNewgame=findViewById(R.id.btnNewgame);
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn4=findViewById(R.id.btn4);
        btn5=findViewById(R.id.btn5);
        btn6=findViewById(R.id.btn6);
        btn7=findViewById(R.id.btn7);
        btn8=findViewById(R.id.btn8);
        btn9=findViewById(R.id.btn9);
    }

    public void check(View view) {
        Button btncurrent = (Button) view;
        // 避免重复点击已落子的按钮
        if(btncurrent.getText().toString().isEmpty()){
            count++;
            if(flag==0){
                btncurrent.setText("X");
                flag=1;
            } else {
                btncurrent.setText("O");
                flag=0;
            }

            // 至少5步才判断胜负
            if(count>4){
                b1=btn1.getText().toString();
                b2=btn2.getText().toString();
                b3=btn3.getText().toString();
                b4=btn4.getText().toString();
                b5=btn5.getText().toString();
                b6=btn6.getText().toString();
                b7=btn7.getText().toString();
                b8=btn8.getText().toString();
                b9=btn9.getText().toString();

                checkWinner();
            }
        }
    }

    private void checkWinner() {
        if(b1.equals(b2) && b2.equals(b3) && !b1.isEmpty()) {
            showWinner(b1);
        }else if (b4.equals(b5) && b5.equals(b6) && !b4.isEmpty()) {
            showWinner(b5);
        }else if (b7.equals(b8) && b8.equals(b9) && !b9.isEmpty()) {
            showWinner(b7);
        }else if (b1.equals(b4) && b4.equals(b7) && !b1.isEmpty()) {
            showWinner(b1);
        }else if (b2.equals(b5) && b5.equals(b8) && !b2.isEmpty()) {
            showWinner(b2);
        }else if (b3.equals(b6) && b6.equals(b9) && !b9.isEmpty()) {
            showWinner(b3);
        }else if (b1.equals(b5) && b5.equals(b9) && !b1.isEmpty()) {
            showWinner(b1);
        }else if (b3.equals(b5) && b5.equals(b7) && !b3.isEmpty()) {
            showWinner(b3);
        } else if (count==9) {
            Toast.makeText(this, "平局", Toast.LENGTH_LONG).show();
            newGame();
        }
    }

    private void showWinner(String winner) {
        Toast.makeText(this, "获胜者是 "+winner, Toast.LENGTH_LONG).show();
        newGame();
    }

    public void newGame(){
        // 重置所有按钮文本
        btn1.setText("");
        btn2.setText("");
        btn3.setText("");
        btn4.setText("");
        btn5.setText("");
        btn6.setText("");
        btn7.setText("");
        btn8.setText("");
        btn9.setText("");
        // 重置计数器和玩家标记
        count=0;
        flag=0;
    }
}

内容的提问来源于stack exchange,提问作者Priyabrata Nayak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 16:44:51