gpt4 book ai didi

java - 由于 ListAdapter 等于 null, Activity 崩溃

转载 作者:行者123 更新时间:2023-12-02 10:10:05 24 4
gpt4 key购买 nike

我为我的应用程序创建了这个适配器:

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 模式(使用传递到此方法的回收 View 作为第二个参数)来实现更平滑的滚动并节省内存。

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>

预期输出: expected output

关于java - 由于 ListAdapter 等于 null, Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com