只有一个活动的简单应用程序在创建布局的TextView并显示与null相同时会产生问题。请点击提交按钮时应用程序崩溃的原因。我在Stack Overflow中尝试了很多解决方案,许多解决方案都说该视图可能位于指定活动之外,但是这里只有一个活动。所以研究对我没有帮助,现在我和那些可以预测我可能犯下的错误的开发人员一起离开了。
MainActivity.java
package me.thirumurugan.quiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button submit;
TextView result;
RadioButton answer_a;
EditText answer_b;
CheckBox answer_c1, answer_c2, answer_c3, answer_c4;
EditText answer_d;
int correct = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
answer_a = (RadioButton) findViewById(R.id.answer_a);
answer_b = (EditText) findViewById(R.id.answer_b);
answer_c1 = (CheckBox) findViewById(R.id.answer_c1);
answer_c2 = (CheckBox) findViewById(R.id.answer_c2);
answer_c3 = (CheckBox) findViewById(R.id.answer_c3);
answer_c4 = (CheckBox) findViewById(R.id.answer_c4);
answer_d = (EditText) findViewById(R.id.answer_d);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
correct = 0;
if (answer_a.isChecked()){
correct++;
}
if (answer_b.getText().toString().equals("2020")){
correct++;
}
if (answer_c1.isChecked()&&answer_c2.isChecked()&&answer_c3.isChecked()&&!answer_c4.isChecked()){
correct++;
}
if (answer_d.getText().toString().toLowerCase().equals("arun jaitley")){
correct++;
}
result.setText("The Score is " + correct + " on 4!");
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="me.thirumurugan.quiz.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_messege"
android:textAlignment="center"
android:id="@+id/result"
android:padding="@dimen/padding"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/colorAccent"
android:textColor="#FFF"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:text="@string/question_1"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/answer_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_1_a" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_1_b" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:layout_marginTop="@dimen/padding"
android:text="@string/question_2"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<EditText
android:id="@+id/answer_b"
android:hint="@string/answer_hint"
android:textColorHint="@color/colorAccent"
android:layout_width="match_parent"
android:textSize="14sp"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
tools:targetApi="lollipop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:layout_marginTop="@dimen/padding"
android:text="@string/question_3"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<CheckBox
android:id="@+id/answer_c1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_a" />
<CheckBox
android:id="@+id/answer_c2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_b" />
<CheckBox
android:id="@+id/answer_c3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_c" />
<CheckBox
android:id="@+id/answer_c4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_d" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:layout_marginTop="@dimen/padding"
android:text="@string/question_4"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<EditText
android:id="@+id/answer_d"
android:hint="@string/answer_hint"
android:textSize="14sp"
android:textColorHint="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
tools:targetApi="lollipop" />
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:text="@string/submit"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:targetApi="lollipop" />
</LinearLayout>
</ScrollView>
</LinearLayout>
跟踪中的错误消息如下:
08-10 21:58:06.301 20947-20947 / me.thirumurugan.quiz E / AndroidRuntime: 致命异常:主要流程:me.thirumurugan.quiz,PID:20947
java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'为null 对象参考 me.thirumurugan.quiz.MainActivity $ 1.onClick(MainActivity.java:52)at at android.view.View.performClick(View.java:5637)at android.view.View $ PerformClick.run(View.java:22429)at android.os.Handler.handleCallback(Handler.java:751)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6119)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)