由于Android Studio中的nullpointerException而导致应用崩溃

时间:2019-01-20 00:36:18

标签: java android-studio nullpointerexception crash

对于我的班级,我必须在Android Studio中开发一个琐事游戏,我认为 我已经弄明白了,但是现在我在模拟器上运行它时,我的应用程序崩溃了。有人可以告诉我怎么回事吗?就是说我在第24行(该行显示mQuestionTv.setText(R.string.question_text1)上有一个nullpointerException;

这就是logcat中的内容:

  

由于:java.lang.NullPointerException:尝试调用虚拟   空对象上的方法'void android.widget.TextView.setText(int)'   参考           在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>

2 个答案:

答案 0 :(得分:0)

移动以下语句:

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

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

答案 1 :(得分:0)

这是来自Android的On create的文档。请注意,如果您重写此方法,则必须调用超类实现。这样做是这样的:

super.onCreate(savedInstanceState);

此后,您需要使用setContentView()方法来扩展视图。它将通过布局资源设置活动内容:

setContentView(R.layout.activity_main);

现在,您将能够在该UI(activity_main)中检索需要以编程方式进行交互的小部件。