java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.widget.TextView.setText(java.lang.CharSequence)'
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object refer
我正在尝试制作一个简单的单屏应用程序来显示列表视图,但发生了这个错误,ReportCard 是 ArrayAdapter class。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity" />
MainActivity.java
package com.vitikasoni.reportcard;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Student> s = new ArrayList<Student>();
s.add(new Student("Vitika", 87, 79, 93, 89));
s.add(new Student("Faizal", 90, 95, 89, 99));
s.add(new Student("Prince", 83, 73, 92, 67));
s.add(new Student("Sejal", 83, 79, 95, 79));
ReportCard r = new ReportCard(this, s);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(r);
}
}
view_view.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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/na"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/m"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/e"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English:98" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/ev"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="EVS:98" />
<TextView
android:id="@+id/h"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hindi:98" />
<TextView
android:id="@+id/p"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Percent:98%" />
</LinearLayout>
</LinearLayout>
Reportcard.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class ReportCard extends ArrayAdapter<Student> {
public ReportCard(Context c, ArrayList<Student> s) {
super(c, 0, s);
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.activity_main, parent, false);
}
Student currentWord = getItem(position);
TextView na = (TextView) listItemView.findViewById(R.id.na);
na.setText(currentWord.getName());
TextView m = (TextView) listItemView.findViewById(R.id.m);
m.setText(currentWord.getMaths());
TextView h = (TextView) listItemView.findViewById(R.id.h);
h.setText(currentWord.getHindi());
TextView e = (TextView) listItemView.findViewById(R.id.e);
e.setText(currentWord.getEng());
TextView ev = (TextView) listItemView.findViewById(R.id.ev);
ev.setText(currentWord.getEvs());
TextView p = (TextView) listItemView.findViewById(R.id.p);
return listItemView;
}
}
Student.java
package com.vitikasoni.reportcard;
public class Student {
private String name;
private int eng;
private int maths;
private int hindi;
private int evs;
private double per;
public Student(String n, int e, int m, int h, int ev) {
name = n;
eng = e;
maths = m;
hindi = h;
evs = ev;
}
public String getName() {
return name;
}
public int getEng() {
return eng;
}
public int getHindi() {
return hindi;
}
public int getEvs() {
return evs;
}
public int getMaths() {
return maths;
}
public double getPer() {
double pr = (eng + hindi + maths + evs) / 4;
return pr;
}
}
您将错误的布局设置为适配器项目视图。
改变这个
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
和
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.view_view, parent, false);
首先为 ListView
适配器设置正确的 layout id
以克服您当前拥有的 Null Pointer Exception
。修复此问题后,您的代码将不会 运行,因为还有另一个问题,您肯定会遇到 android.content.res.Resources$NotFoundException: String resource ID #0x4f
异常。
您的 setText(currentWord.getMaths())
行有问题,因为此处 currentWord.getMaths()
是 integer
类型,而 setText(integer)
将此 interger
参数视为 R.string.someName
。
所以要么将 private int maths;
更改为 private String maths;
您必须将所有 interger
类型更改为 String
类型。
或
在适配器的 getView()
方法中使用以下代码。
// 这里我将 integer
连接到 String
并设置为 TextView
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.view_view, parent, false);
}
Student currentWord = getItem(position);
TextView na = (TextView) listItemView.findViewById(R.id.na);
na.setText(currentWord.getName());
TextView m = (TextView) listItemView.findViewById(R.id.m);
m.setText(currentWord.getMaths()+"");
TextView h = (TextView) listItemView.findViewById(R.id.h);
h.setText(currentWord.getHindi()+"");
TextView e = (TextView) listItemView.findViewById(R.id.e);
e.setText(currentWord.getEng()+"");
TextView ev = (TextView) listItemView.findViewById(R.id.ev);
ev.setText(currentWord.getEvs()+"");
TextView p = (TextView) listItemView.findViewById(R.id.p);
return listItemView;
}
我正在尝试制作一个简单的单屏应用程序来显示列表视图,但发生了这个错误,ReportCard 是 ArrayAdapter class。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity" />
MainActivity.java
package com.vitikasoni.reportcard;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Student> s = new ArrayList<Student>();
s.add(new Student("Vitika", 87, 79, 93, 89));
s.add(new Student("Faizal", 90, 95, 89, 99));
s.add(new Student("Prince", 83, 73, 92, 67));
s.add(new Student("Sejal", 83, 79, 95, 79));
ReportCard r = new ReportCard(this, s);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(r);
}
}
view_view.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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/na"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/m"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/e"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English:98" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/ev"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="EVS:98" />
<TextView
android:id="@+id/h"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hindi:98" />
<TextView
android:id="@+id/p"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Percent:98%" />
</LinearLayout>
</LinearLayout>
Reportcard.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class ReportCard extends ArrayAdapter<Student> {
public ReportCard(Context c, ArrayList<Student> s) {
super(c, 0, s);
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.activity_main, parent, false);
}
Student currentWord = getItem(position);
TextView na = (TextView) listItemView.findViewById(R.id.na);
na.setText(currentWord.getName());
TextView m = (TextView) listItemView.findViewById(R.id.m);
m.setText(currentWord.getMaths());
TextView h = (TextView) listItemView.findViewById(R.id.h);
h.setText(currentWord.getHindi());
TextView e = (TextView) listItemView.findViewById(R.id.e);
e.setText(currentWord.getEng());
TextView ev = (TextView) listItemView.findViewById(R.id.ev);
ev.setText(currentWord.getEvs());
TextView p = (TextView) listItemView.findViewById(R.id.p);
return listItemView;
}
}
Student.java
package com.vitikasoni.reportcard;
public class Student {
private String name;
private int eng;
private int maths;
private int hindi;
private int evs;
private double per;
public Student(String n, int e, int m, int h, int ev) {
name = n;
eng = e;
maths = m;
hindi = h;
evs = ev;
}
public String getName() {
return name;
}
public int getEng() {
return eng;
}
public int getHindi() {
return hindi;
}
public int getEvs() {
return evs;
}
public int getMaths() {
return maths;
}
public double getPer() {
double pr = (eng + hindi + maths + evs) / 4;
return pr;
}
}
您将错误的布局设置为适配器项目视图。
改变这个
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
和
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.view_view, parent, false);
首先为 ListView
适配器设置正确的 layout id
以克服您当前拥有的 Null Pointer Exception
。修复此问题后,您的代码将不会 运行,因为还有另一个问题,您肯定会遇到 android.content.res.Resources$NotFoundException: String resource ID #0x4f
异常。
您的 setText(currentWord.getMaths())
行有问题,因为此处 currentWord.getMaths()
是 integer
类型,而 setText(integer)
将此 interger
参数视为 R.string.someName
。
所以要么将 private int maths;
更改为 private String maths;
您必须将所有 interger
类型更改为 String
类型。
或
在适配器的 getView()
方法中使用以下代码。
// 这里我将 integer
连接到 String
并设置为 TextView
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.view_view, parent, false);
}
Student currentWord = getItem(position);
TextView na = (TextView) listItemView.findViewById(R.id.na);
na.setText(currentWord.getName());
TextView m = (TextView) listItemView.findViewById(R.id.m);
m.setText(currentWord.getMaths()+"");
TextView h = (TextView) listItemView.findViewById(R.id.h);
h.setText(currentWord.getHindi()+"");
TextView e = (TextView) listItemView.findViewById(R.id.e);
e.setText(currentWord.getEng()+"");
TextView ev = (TextView) listItemView.findViewById(R.id.ev);
ev.setText(currentWord.getEvs()+"");
TextView p = (TextView) listItemView.findViewById(R.id.p);
return listItemView;
}