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

Android测验应用检查用户答案时崩溃及判断逻辑异常求助

分析你的Android测验应用崩溃&逻辑失效问题

我看了你的代码,能发现几个导致崩溃和逻辑不对的关键问题,咱们一个个来解决:

1. 空指针异常(最可能的崩溃原因)

在checkIfRight()方法里,你只在答案正确的分支里初始化了score这个TextView:

if (rightAnswers[count2].equals(buttonText)) {
    score = findViewById(R.id.score); // 只有这里才找控件
    score.setText("right");
} else{
    score.setText("wrong"); // 这里score可能还是null!
}

如果答案不对,score还没被赋值,直接调用setText()就会触发空指针崩溃。另外,我看你的XML布局里没贴出id="score"的TextView,如果这个控件根本没在布局里定义,那findViewById永远返回null,不管对错都会崩溃。

2. 按钮文本获取逻辑错误

你在a1()、a2()、a3()里反复给全局变量btn赋值,最后btn指向的是第三个按钮(q3)。不管用户点击的是q1、q2还是q3,checkIfRight()都会取q3的文本去对比,这完全不符合你的需求——你应该获取被点击的那个按钮的文本才对。

解决方法是利用onClick(View v)里的v参数,它就是被点击的控件:

public void onClick(View v) {
    // 先获取点击按钮的文本
    buttonText = ((Button)v).getText().toString();
    // 然后再执行其他逻辑...
}

3. 数组索引越界风险

你的count初始值是1,但数组的索引是从0开始的。第一次点击按钮后,count变成2,会直接跳到第三个问题,而且当count超过quesitons.length-1时,就会触发数组越界崩溃。应该把count和count2的初始值改成0。

另外,每次点击都要检查count是否已经到了数组末尾,避免越界:

public void onClick(View v) {
    if (count >= quesitons.length) {
        // 已经到最后一题了,提示用户或者重置
        Toast.makeText(this, "测验结束!", Toast.LENGTH_SHORT).show();
        return;
    }
    // 其他逻辑...
}

4. 方法执行顺序混乱

你现在的onClick()里先调用a1()、a2()、a3()更新按钮文本,然后才调用checkIfRight(),这时候对比的是更新后的按钮文本,而不是用户点击的那个旧文本!顺序完全搞反了,应该先检查答案,再更新下一题的内容。

修正后的核心代码

我把关键部分的代码修正了,你可以参考:

Java类修正片段

// 修正初始值为0
int count = 0; 
int count2 = 0;
String buttonText;
TextView score; // 全局声明,提前初始化

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_categories);
    // 提前初始化score控件,避免空指针
    score = findViewById(R.id.score);
    // 初始化第一题的内容
    getQuestions();
    a1();
    a2();
    a3();
}

// 修正拼写错误:quesitons -> questions
String[] questions = { 
    "What was the tallest Dam as of 2017?",
    "How many people did the South Fork Dam kill in 1889?",
    "What do Dams generate?",
    "By how much percent did freshwater species decline by due to dams since 1970?",
    "Around what time was the first dam built?",
    "How many large(15m+) dams are in existence today?",
    "The average dam can be expected to cost how much?"
};

public void checkIfRight() {
    if (rightAnswers[count2].equals(buttonText)) {
        score.setText("right");
    } else {
        score.setText("wrong");
    }
}

public void onClick(View v) {
    // 先判断是否到最后一题
    if (count >= questions.length) {
        Toast.makeText(this, "All questions done!", Toast.LENGTH_SHORT).show();
        return;
    }

    // 获取被点击按钮的文本
    buttonText = ((Button)v).getText().toString();
    // 检查答案
    checkIfRight();

    // 更新计数,准备下一题
    count++;
    count2++;

    // 如果还没到最后,更新题目和选项
    if (count < questions.length) {
        getQuestions();
        a1();
        a2();
        a3();
    }
}

布局补充

确保你的XML里添加了score的TextView,比如放在合适的位置:

<TextView
    android:id="@+id/score"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/mytext"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:textColor="#FF0000"/>

最后,还有个小细节:Damn应该是Dam(拼写错误),虽然不影响运行,但建议修正,保持专业。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:08:19