我一直得到相同的错误。我知道这是一个重复的问题,但没有任何帮助。这个应用程序应该输出一些数据从CSV文件与每次点击。我知道错误意味着textview是空的。但id匹配我的xml和java文件。谁能告诉我,我哪里错了?
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.decisionmap.MainActivity.onCreate(MainActivity.java:68)
我在
得到错误
option.setText(node.getOption());
option2.setText(node.getOption2());
公共类MainActivity扩展AppCompatActivity {
private TextView textView;
private Button bn1;
private Button bt2;
private TextView option1;
private TextView option2;
DecisionNode node;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.description);
bn1 = (Button) findViewById(R.id.button1);
bn2 = (Button) findViewById(R.id.button2);
DecisionMap crush;
try {
map = new DecisionMap(getApplicationContext());
} catch (
FileNotFoundException e) {
e.printStackTrace();
return;
}
node = map.entryPoint();
textView.setText(node.getDescription());
option1.setText(node.getOption());
option2.setText(node.getOption2());
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:layout_height="match_parent"
android:background="#1A2D30"
tools:context=".MainActivity">
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "textView"
/>
<TextView
android:id="@+id/firstOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "textView">
</TextView>
<TextView
android:id="@+id/secondOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "textView">
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="118dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="180dp"
android:text="@string/app_name"
app:backgroundTint="#ACA248" />
<Button
android:id="@+id/button2"
android:layout_width="118dp"
android:layout_height="62dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="168dp"
android:text="@string/app_name"
android:textColor="#FFFFFF"
app:backgroundTint="#AEA343" />
<TextView
android:id="@+id/textView"
android:layout_width="259dp"
android:layout_height="172dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="125dp"
android:background="#215023"
android:text="@string/app_name" />
</RelativeLayout>
option1和option2为空,因为您从未初始化它们。你所需要做的就是:
option1 = (TextView) findViewById(R.id.option1);
option2 = (TextView) findViewById(R.id.option2);
option1.setText(node.getOption());
...