简体   繁体   English

由于 android Studio 中的 nullpointerException 导致应用程序崩溃

[英]App crashing due to nullpointerException in android Studio

For my class I have to develop a trivia game in Android Studio and I thought I got it all figured out but now my app crashes as soon as I run it on the emulator.对于我的 class,我必须在 Android Studio 中开发一个琐事游戏,我以为我已经弄清楚了,但是现在我的应用程序在我在模拟器上运行时立即崩溃。 Can somebody tell me what's going on?有人可以告诉我发生了什么事吗? It's saying i have a nullpointerException on line 24 (The line that says mQuestionTv.setText(R.string.question_text1 );这就是说我在第 24 行有一个 nullpointerException(表示mQuestionTv.setText(R.string.question_text1的行);

This is what it says in logcat:这就是它在 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) 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 )

Here is my java code for the app:这是我的应用程序的 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);
}
}

Here is the xml code for my string resources:这是我的字符串资源的 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>

Main Xml layout code:主要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>

Move these statements: 移动以下语句:

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

before any other statement inside onCreate() . onCreate()任何其他语句之前。
Any findViewById() method you use to initialize your views must be called after the layout is inflated otherwise the views are null . 您必须在布局膨胀调用用于初始化视图的任何findViewById()方法,否则视图为null

Here is the documentation of On create from android. 这是关于从Android 创建的文档。 Note that If you override this method you must call through to the superclass implementation. 请注意,如果您重写此方法,则必须调用超类实现。 which is done like this: 这样做是这样的:

super.onCreate(savedInstanceState);

After this, you need to inflate your view using setContentView() method. 此后,您需要使用setContentView()方法来扩展视图。 which will set the activity content from a layout resource: 这将从布局资源中设置活动内容:

setContentView(R.layout.activity_main);

Now you will be able to retrieve the widgets in that UI ( activity_main ) which you need to interact with programmatically. 现在,您将能够在该UI( activity_main )中检索需要以编程方式进行交互的小部件。

rivate int[] mQuestionBank = new int[] {R.string.question_text1, R.string.question_text2, R.string.question_text3}; rivate int[] mQuestionBank = new int[] {R.string.question_text1, R.string.question_text2, R.string.question_text3};

Resources are string.资源是字符串。 Typecast resources to integer.将资源类型转换为 integer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM