Java Android - 自定义适配器 getCount 方法 nullPointer 错误

Posted

技术标签:

【中文标题】Java Android - 自定义适配器 getCount 方法 nullPointer 错误【英文标题】:Java Android - Custom Adapter getCount Method nullPointer Error 【发布时间】:2022-01-05 02:42:01 【问题描述】:

我正在为扩展基本适配器的ListView 编写自定义适配器。基本上,它会列出一些名字、姓氏等。

适配器类中的getSize() 方法正在获取NullPointerException。 我不知道发生了什么。

2021-11-27 13:55:36.561 27997-27997/com.example.listviewlesson E/androidRuntime: FATAL EXCEPTION: main
    Process: com.example.listviewlesson, PID: 27997
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.listviewlesson/com.example.listviewlesson.MainActivity: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.example.listviewlesson.UserListAdapter.getCount(**UserListAdapter.java:27**)
        at android.widget.ListView.setAdapter(ListView.java:575)
        at com.example.listviewlesson.MainActivity.fillList(**MainActivity.java:26**)
        at com.example.listviewlesson.MainActivity.onCreate(**MainActivity.java:21**)
        at android.app.Activity.performCreate(Activity.java:7136)

这是我的适配器类

package com.example.listviewlesson;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class UserListAdapter extends BaseAdapter 

Context context;
List<UserModel> uList;

public UserListAdapter(List<UserModel> list, Context context) 
    this.uList = list;
    this.context = context;



@Override
public int getCount() 

    return uList.size();


@Override
public Object getItem(int position) 
    return uList.get(position);


@Override
public long getItemId(int position) 
    return 0;


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    View layout = LayoutInflater.from(context).inflate(R.layout.layout, parent, false);

    TextView name = (TextView) layout.findViewById(R.id.namexml);
    TextView surname = (TextView) layout.findViewById(R.id.surnamexml);
    TextView age = (TextView) layout.findViewById(R.id.agexml);
    TextView team = (TextView) layout.findViewById(R.id.teamxml);

    name.setText(uList.get(position).getName());
    surname.setText(uList.get(position).getSurname());
    age.setText(uList.get(position).getAge());
    team.setText(uList.get(position).getTeam());

    return layout;


这是主要活动

package com.example.listviewlesson;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity 
List<UserModel> userList;
UserListAdapter adp;
ListView lview;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Tanimla();
    fillList();


public void fillList() 
    userList = new ArrayList<UserModel>(userList);
    UserModel model0 = new UserModel("USER1", "SRNAME", "29", "BJK");
    UserModel model1 = new UserModel("USER2", "SRNAME", "29", "BJK");
    UserModel model2 = new UserModel("USER3", "SRNAME", "1", "BJK");

    userList.add(model0);
    userList.add(model1);
    userList.add(model2);


    adp = new UserListAdapter(userList, this);
    lview.setAdapter(adp);

void Tanimla() 
    lview = (ListView) findViewById(R.id.listview0);




这是主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">


<ListView
    android:layout_
    android:layout_
    android:id="@+id/listview0"
    />

【问题讨论】:

【参考方案1】:

一切正常,只需在fillList() 方法中更改这行代码即可。

 public void fillList() 
    userList = new ArrayList<>();
    UserModel model0 = new UserModel("USER1", "SRNAME", "29", "BJK");
    UserModel model1 = new UserModel("USER2", "SRNAME", "29", "BJK");
    UserModel model2 = new UserModel("USER3", "SRNAME", "1", "BJK");

    userList.add(model0);
    userList.add(model1);
    userList.add(model2);


    adp = new UserListAdapter(userList, this);
    lview.setAdapter(adp);

【讨论】:

以上是关于Java Android - 自定义适配器 getCount 方法 nullPointer 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用自定义适配器自定义 Android ListView

Android:自定义列表视图无法正常工作

在 Android 中使用 SearchableSpinner 和自定义适配器

Android 中的自定义适配器

带有 MultiTouch 的 Android 图库适配器?自定义图库还是自定义 ImageView?

自定义适配器 - Android Studio