Activity 崩溃,因为 ListAdapter 等于 null
Activity crashing because ListAdapter equals null
我为我的应用程序创建了这个适配器:
public class DeadlineAdapter extends ArrayAdapter<Deadline> {
private Activity context;
public List<Deadline> deadlineList;
public DeadlineAdapter(Activity context, List<Deadline> deadlineList){
super(context, R.layout.item_deadline, deadlineList);
this.context = context;
this.deadlineList = deadlineList;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listView = inflater.inflate(R.layout.item_deadline, parent, false);
TextView displayDate = listView.findViewById(R.id.display_deadline_date_tv);
TextView displayName = listView.findViewById(R.id.display_deadline_name_tv);
Deadline deadline = deadlineList.get(position);
displayDate.setText(deadline.getDate());
displayName.setText(deadline.getDeadlineName());
return listView;
}
}
但每次我尝试 运行 该应用程序时,我的应用程序都会崩溃。日志内容如下:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
我该如何解决这个问题?
检查我的例子。我只使用 String
作为列表中的元素。
并且请记住,您应该使用 View Holder 模式(使用作为第二个参数传递给此方法的回收视图)来实现更平滑的滚动和节省内存。
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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ActivityMain.java
public class ActivityMain extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUi();
}
private void setupUi() {
List<String> elements = new ArrayList<>(Arrays.asList(
"Red",
"Blue",
"Green",
"Black"
));
DeadlineAdapter adapter = new DeadlineAdapter(this, elements);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
和适配器 - DeadlineAdapter.java:
public class DeadlineAdapter extends ArrayAdapter<String> {
private Activity context;
private List<String> deadlineList;
public DeadlineAdapter(Activity context, List<String> deadlineList) {
super(context, R.layout.item_deadline, deadlineList);
this.context = context;
this.deadlineList = deadlineList;
}
@NonNull
public View getView(int position, View convertView, @NotNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listView = inflater.inflate(R.layout.item_deadline, parent, false);
TextView positionTv = listView.findViewById(R.id.row_id);
TextView textTv = listView.findViewById(R.id.row_text);
positionTv.setText(String.format("%s.", position + 1));
textTv.setText(deadlineList.get(position));
return listView;
}
}
和列表项 - item_deadline:
<?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:padding="30dp">
<TextView
android:id="@+id/row_id"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/row_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
预期输出:
我为我的应用程序创建了这个适配器:
public class DeadlineAdapter extends ArrayAdapter<Deadline> {
private Activity context;
public List<Deadline> deadlineList;
public DeadlineAdapter(Activity context, List<Deadline> deadlineList){
super(context, R.layout.item_deadline, deadlineList);
this.context = context;
this.deadlineList = deadlineList;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listView = inflater.inflate(R.layout.item_deadline, parent, false);
TextView displayDate = listView.findViewById(R.id.display_deadline_date_tv);
TextView displayName = listView.findViewById(R.id.display_deadline_name_tv);
Deadline deadline = deadlineList.get(position);
displayDate.setText(deadline.getDate());
displayName.setText(deadline.getDeadlineName());
return listView;
}
}
但每次我尝试 运行 该应用程序时,我的应用程序都会崩溃。日志内容如下:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
我该如何解决这个问题?
检查我的例子。我只使用 String
作为列表中的元素。
并且请记住,您应该使用 View Holder 模式(使用作为第二个参数传递给此方法的回收视图)来实现更平滑的滚动和节省内存。
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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ActivityMain.java
public class ActivityMain extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUi();
}
private void setupUi() {
List<String> elements = new ArrayList<>(Arrays.asList(
"Red",
"Blue",
"Green",
"Black"
));
DeadlineAdapter adapter = new DeadlineAdapter(this, elements);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
和适配器 - DeadlineAdapter.java:
public class DeadlineAdapter extends ArrayAdapter<String> {
private Activity context;
private List<String> deadlineList;
public DeadlineAdapter(Activity context, List<String> deadlineList) {
super(context, R.layout.item_deadline, deadlineList);
this.context = context;
this.deadlineList = deadlineList;
}
@NonNull
public View getView(int position, View convertView, @NotNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listView = inflater.inflate(R.layout.item_deadline, parent, false);
TextView positionTv = listView.findViewById(R.id.row_id);
TextView textTv = listView.findViewById(R.id.row_text);
positionTv.setText(String.format("%s.", position + 1));
textTv.setText(deadlineList.get(position));
return listView;
}
}
和列表项 - item_deadline:
<?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:padding="30dp">
<TextView
android:id="@+id/row_id"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/row_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
预期输出: