活动崩溃,因为ListAdapter等于null

时间:2019-03-08 15:12:36

标签: java android android-arrayadapter

我为我的应用创建了此适配器:

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;
    }
}

但是,每次我尝试运行该应用程序时,我的应用程序都会崩溃。日志显示以下内容:

  

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.ListView.setAdapter(android.widget.ListAdapter)”

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

检查我的示例。我只使用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>

预期输出: expected output