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

标签 java android-studio nullpointerexception crash

对于我的类(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/

相关文章:

android - 创建包含 javadoc 和源的 Android 库 AAR

android - Android Studio 3.2.1 中的 Gradle 项目同步失败

java - 什么是NullPointerException,我该如何解决?

java - 从.jar加载图像,空指针异常

java - 获取资源时出现 NullPointerException

java - 如何将对象映射到字符串中以使用 ObjectMapper 进行设置

java - WildFly 独立版无法运行

java - 从表迁移到网格 vaadin

android - 为什么发布版本变体总是可调试的?

Java文件读取和字符串分割问题