我正在开发一个简单的测验应用,向用户提出问题并可以选择True或False来回答问题。我添加了一个“显示答案”按钮,但当我点击该按钮时,我得到以下NullPointerException。我想我在activity_cheat.xml中遗漏了一些内容,但我无法弄清楚是什么。
05-31 19:30:59.238 5978-5978/com.bignerdranch.android.geoquiz.geoquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bignerdranch.android.geoquiz.geoquiz, PID: 5978
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
at com.bignerdranch.android.geoquiz.geoquiz.CheatActivity$1.onClick(CheatActivity.java:33)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
CheatActivity.java
package com.bignerdranch.android.geoquiz.geoquiz;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends Activity {
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
} else {
mAnswerTextView.setText(R.string.false_button);
}
}
});
}
}
activity_cheat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text_view" />
<TextView
android:id="@+id/answerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<Button
android:id="@+id/showAnswerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_answer_button" />
</LinearLayout>
答案 0 :(得分:7)
您忘记将mAnswerTextView绑定到UI:
添加
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
行后
setContentView(R.layout.activity_cheat);
答案 1 :(得分:6)
mAnswerTextView
永远不会被初始化。这就是你获得NPE的原因。
添加
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
之前
if (mAnswerIsTrue)
答案 2 :(得分:6)
mAnswerTextView
为空。
添加,
mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
。