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

Android Studio Dolphin中运行真假问答APP无界面显示问题求助

问题解决:Android问答APP运行后屏幕空白

核心错误原因

应用运行后屏幕空白,本质是Activity初始化阶段的空指针异常:

  • 在MainActivity中,你直接在类成员变量位置调用findViewById,此时Activity还未执行onCreate,布局还未加载,findViewById会返回null。后续在onCreate中调用textopregunta.setText()时,会触发NullPointerException,导致应用静默崩溃,表现为屏幕无内容。

修复方案

步骤1:修正成员变量初始化时机

将控件初始化代码移到onCreate方法内部,且必须在setContentView(R.layout.activity_main)之后执行。可以使用lateinit关键字延迟初始化成员变量,避免提前调用findViewById。

步骤2:(可选)优化代码逻辑

  • 用索引变量跟踪当前题目位置,替代通过文本匹配判断题目的低效逻辑。
  • 简化Pregunta类为数据类,减少冗余代码。

修复后的完整代码

1. 修正后的MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    // 使用lateinit延迟初始化控件
    private lateinit var buzz: Buzz
    private lateinit var textopregunta: TextView
    private lateinit var textorepuesta: TextView
    private lateinit var pulsarverdadero: Button
    private lateinit var pulsarfalso: Button
    private lateinit var pulsaranterior: Button
    private lateinit var pulsarsiguiente: Button
    // 添加索引变量跟踪当前题目位置
    private var currentQuestionIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 在布局加载后初始化控件
        buzz = Buzz()
        textopregunta = findViewById(R.id.pregunta)
        textorepuesta = findViewById(R.id.respuesta)
        pulsarverdadero = findViewById(R.id.verdadero)
        pulsarfalso = findViewById(R.id.falso)
        pulsaranterior = findViewById(R.id.anterior)
        pulsarsiguiente = findViewById(R.id.siguiente)

        // 加载初始题目
        updateQuestion()
    }

    // 更新当前题目显示
    private fun updateQuestion() {
        textopregunta.text = buzz.darPregunta(currentQuestionIndex)
        textorepuesta.text = "Respuesta" // 重置答案提示
    }

    fun siguente(view: View) {
        currentQuestionIndex = (currentQuestionIndex + 1) % buzz.listaPreguntas.size
        updateQuestion()
    }

    fun anterior(view: View) {
        currentQuestionIndex = if (currentQuestionIndex == 0) {
            buzz.listaPreguntas.size - 1
        } else {
            currentQuestionIndex - 1
        }
        updateQuestion()
    }

    // 真假按钮的点击逻辑
    fun onVerdaderoClick(view: View) {
        checkAnswer(true)
    }

    fun onFalsoClick(view: View) {
        checkAnswer(false)
    }

    private fun checkAnswer(userAnswer: Boolean) {
        val correctAnswer = buzz.listaPreguntas[currentQuestionIndex].verResultado()
        textorepuesta.text = if (userAnswer == correctAnswer) "¡Correcto!" else "Incorrecto"
    }
}

2. 优化后的Pregunta.kt(可选)

// 使用数据类简化代码
data class Pregunta(val pregunta: String, val resultado: Boolean) {
    fun verPregunta(): String = pregunta
    fun verResultado(): Boolean = resultado
}

3. 修正XML中的按钮点击事件

如果添加了真假按钮的点击逻辑,需要在XML中给按钮补充onClick属性:

<Button
    android:id="@+id/verdadero"
    ...
    android:onClick="onVerdaderoClick" />

<Button
    android:id="@+id/falso"
    ...
    android:onClick="onFalsoClick" />

验证修复效果

  1. 重新编译运行应用,此时布局会正常加载,初始题目会显示在屏幕上。
  2. 点击「Anterior」「Siguiente」按钮可以切换题目,点击「Verdadero」「Falso」可以验证答案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 01:01:00