gpt4 book ai didi

java - 由于 android Studio 中的 nullpointerException,应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 07:46:22 24 4
gpt4 key购买 nike

对于我的类(class),我必须在 Android Studio 中开发一个问答游戏,我想我已经弄清楚了一切,但现在我的应用程序在模拟器上运行时就会崩溃。有人能告诉我发生了什么事吗?它说我在第 24 行有一个 nullpointerException (该行显示 mQuestionTv.setText(R.string.question_text1);

这是 logcat 中的内容:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference at edu.unf.n01044854.unftrivia.MainActivity.onCreate(MainActivity.java:24)

这是我的应用程序的 java 代码:

package edu.unf.n01044854.unftrivia;

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

public class MainActivity extends AppCompatActivity {

private TextView mQuestionTv;
private TextView mAnswerTv;
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private int mCurrentIndex = 0;
private int[] mQuestionBank = new int[] {R.string.question_text1,
R.string.question_text2,
R.string.question_text3};
private boolean[] mAnswerBank = new boolean[] {true, false, true};

@Override
protected void onCreate(Bundle savedInstanceState) {
mQuestionTv = (TextView)findViewById(R.id.question_textView);
mQuestionTv.setText(R.string.question_text1);
mAnswerTv = (TextView)findViewById(R.id.answer_textView);

mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAnswerBank[mCurrentIndex])
mAnswerTv.setText(R.string.correct_text);
else
mAnswerTv.setText(R.string.incorrect_text);
}
});

mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mAnswerBank[mCurrentIndex])
mAnswerTv.setText(R.string.correct_text);
else
mAnswerTv.setText(R.string.incorrect_text);
}
});

mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mCurrentIndex == 3)
mCurrentIndex = 0;
else
mCurrentIndex++;
mQuestionTv.setText(mQuestionBank[mCurrentIndex]);
}
});

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

这是我的字符串资源的 xml 代码:

<resources>
<string name="app_name">UNF Trivia</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="next_button">Next</string>
<string name="answer_text">Correct/Incorret</string>
<string name="correct_text">Correct</string>
<string name="incorrect_text">Incorrect</string>
<string name="question_text1">Full-stage opera productions
are offered by student performers at UNF.</string>
<string name="question_text2">The armadillo was never
considered for the mascot adoption at UNF.</string>
<string name="question_text3">UNF offers Open Zip [Line] Nights for
students.</string>
</resources>

主要Xml布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/question_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>

<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>

<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"/>

<TextView
android:id="@+id/answer_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer_text"/>

</LinearLayout>

最佳答案

移动这些语句:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

onCreate() 内的任何其他语句之前。
用于初始化 View 的任何 findViewById() 方法都必须在布局膨胀之后调用,否则 View 将为 null

关于java - 由于 android Studio 中的 nullpointerException,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com