猿问

活动崩溃,因为 ListAdapter 等于 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;

    }

}

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


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


我该如何解决这个问题?


湖上湖
浏览 185回答 1
1回答

红颜莎娜

检查我的例子。我使用String的只是列表中的元素。请记住,您应该使用View Holder 模式(使用传递给此方法的回收视图作为第二个参数)以实现更平滑的滚动和节省内存。activity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <ListView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/list_view"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent" /></LinearLayout>ActivityMain.javapublic class ActivityMain extends AppCompatActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; setupUi();&nbsp; &nbsp; }&nbsp; &nbsp; private void setupUi() {&nbsp; &nbsp; &nbsp; &nbsp; List<String> elements = new ArrayList<>(Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Red",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Blue",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Green",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Black"&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; DeadlineAdapter adapter = new DeadlineAdapter(this, elements);&nbsp; &nbsp; &nbsp; &nbsp; ListView listView = findViewById(R.id.list_view);&nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(adapter);&nbsp; &nbsp; }}和适配器 - DeadlineAdapter.java:public class DeadlineAdapter extends ArrayAdapter<String> {&nbsp; &nbsp; private Activity context;&nbsp; &nbsp; private List<String> deadlineList;&nbsp; &nbsp; public DeadlineAdapter(Activity context, List<String> deadlineList) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, R.layout.item_deadline, deadlineList);&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; this.deadlineList = deadlineList;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; public View getView(int position, View convertView, @NotNull ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater = context.getLayoutInflater();&nbsp; &nbsp; &nbsp; &nbsp; View listView = inflater.inflate(R.layout.item_deadline, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; TextView positionTv = listView.findViewById(R.id.row_id);&nbsp; &nbsp; &nbsp; &nbsp; TextView textTv = listView.findViewById(R.id.row_text);&nbsp; &nbsp; &nbsp; &nbsp; positionTv.setText(String.format("%s.", position + 1));&nbsp; &nbsp; &nbsp; &nbsp; textTv.setText(deadlineList.get(position));&nbsp; &nbsp; &nbsp; &nbsp; return listView;&nbsp; &nbsp; }}和项目列表 - item_deadline:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:padding="30dp">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/row_id"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="100dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/row_text"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" /></LinearLayout>预期输出:检查我的例子。我使用String的只是列表中的元素。请记住,您应该使用View Holder 模式(使用传递给此方法的回收视图作为第二个参数)以实现更平滑的滚动和节省内存。activity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <ListView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/list_view"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent" /></LinearLayout>ActivityMain.javapublic class ActivityMain extends AppCompatActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; setupUi();&nbsp; &nbsp; }&nbsp; &nbsp; private void setupUi() {&nbsp; &nbsp; &nbsp; &nbsp; List<String> elements = new ArrayList<>(Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Red",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Blue",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Green",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Black"&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; DeadlineAdapter adapter = new DeadlineAdapter(this, elements);&nbsp; &nbsp; &nbsp; &nbsp; ListView listView = findViewById(R.id.list_view);&nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(adapter);&nbsp; &nbsp; }}和适配器 - DeadlineAdapter.java:public class DeadlineAdapter extends ArrayAdapter<String> {&nbsp; &nbsp; private Activity context;&nbsp; &nbsp; private List<String> deadlineList;&nbsp; &nbsp; public DeadlineAdapter(Activity context, List<String> deadlineList) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, R.layout.item_deadline, deadlineList);&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; this.deadlineList = deadlineList;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; public View getView(int position, View convertView, @NotNull ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater = context.getLayoutInflater();&nbsp; &nbsp; &nbsp; &nbsp; View listView = inflater.inflate(R.layout.item_deadline, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; TextView positionTv = listView.findViewById(R.id.row_id);&nbsp; &nbsp; &nbsp; &nbsp; TextView textTv = listView.findViewById(R.id.row_text);&nbsp; &nbsp; &nbsp; &nbsp; positionTv.setText(String.format("%s.", position + 1));&nbsp; &nbsp; &nbsp; &nbsp; textTv.setText(deadlineList.get(position));&nbsp; &nbsp; &nbsp; &nbsp; return listView;&nbsp; &nbsp; }}和项目列表 - item_deadline:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:padding="30dp">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/row_id"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="100dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/row_text"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" /></LinearLayout>预期输出:
随时随地看视频慕课网APP

相关分类

Java
我要回答