对于我的课程,我必须在 Android Studio 中开发一个琐事游戏,我以为我已经弄清楚了,但是现在我的应用程序在我在模拟器上运行时立即崩溃。有人可以告诉我发生了什么事吗?意思是我在第 24 行有一个 nullpointerException (上面写着的那一行mQuestionTv.setText(R.string.question_text1);
这就是它在 logcat 中所说的:
原因:java.lang.NullPointerException:尝试在 edu.unf.n01044854.unftrivia.MainActivity.onCreate(MainActivity.java:24) 的空对象引用上调用虚拟方法“void android.widget.TextView.setText(int)” )
这是我的应用程序的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);
}
});
当年话下
猛跑小猪
相关分类