ExpandableListView:尝试在空对象引用上调用接口方法 'int java.util.List.size()'

ExpandableListView: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

说明问题出在这里:

@Override
public int getChildrenCount(int groupPosition) {
    return hashMap.get(groupPosition).size();
}

我尝试打印地图尺寸,结果确实如此。它只有两个键:"Upcoming Events" 和 "Past Events"。我使用 containsKey() 进行了检查,结果为真。但不知何故,我仍然无法获得作为值包含的 List 对象的大小。

知道我没有为此创建另一个 class 文件并且所有代码都捆绑在同一个 class 文件中可能会有所帮助。

这是我的代码:

class CustomEventListAdapter extends BaseExpandableListAdapter{
    HashMap<String, List<Event>> hashMap;
    Context con;
    CustomEventListAdapter(Context context, HashMap<String, List<Event>> map){
        hashMap = map;
        con = context;
    }

    List<String> groupTitle = Arrays.asList("Upcoming Events","Past Events");

    @Override
    public int getGroupCount() {
        return groupTitle.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return hashMap.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupTitle.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return hashMap.get(groupTitle.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.groupHeader);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(groupTitle.get(groupPosition));
        return convertView;        }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_item, null);
        }
        TextView eventName = (TextView) convertView.findViewById(R.id.eventName);
        TextView eventDate = (TextView) convertView.findViewById(R.id.eventDate);
        TextView eventDesc = (TextView) convertView.findViewById(R.id.eventDesc);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.eventPic);

        eventName.setText(hashMap.get(groupPosition).get(childPosition).getName());
        eventDate.setText(hashMap.get(groupPosition).get(childPosition).getDate());
        eventDesc.setText(hashMap.get(groupPosition).get(childPosition).getDetails());

        return convertView;        }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

这是 onCreate() :

   expandableListView = (ExpandableListView) findViewById(R.id.eventList);
    hashMap  = new HashMap<>();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference reference = database.getReference().child("Events");
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<Event> eventList = new ArrayList<>();
            //List<Event> upcoming = new ArrayList<>();
            //List<Event> past = new ArrayList<>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                Event event = new Event();
                event.name = String.valueOf(postSnapshot.child("Name").getValue());
                event.details = String.valueOf(postSnapshot.child("Details").getValue());
                event.add = String.valueOf(postSnapshot.child("PhotoAdd").getValue());
                event.date = String.valueOf(postSnapshot.child("Date").getValue());

                eventList.add(event);
                Toast.makeText(EventsActivity.this, "longDate = " + event.getLongDate() , Toast.LENGTH_SHORT).show();


            }
            long current = Calendar.getInstance().getTimeInMillis();
            Collections.sort(eventList, new Sortbyroll());
            int i=0;
            for( i=0; i< eventList.size(); i++){
                if(eventList.get(i).getLongDate() < current){
                    break;
                }
            }
            hashMap.put("Upcoming Events", eventList.subList(0,i));
            hashMap.put("Past Events", eventList.subList(i,eventList.size()));

            ExpandableListAdapter listAdapter = new CustomEventListAdapter(getBaseContext(),hashMap);
            expandableListView.setAdapter(listAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

也许您放在这里的代码不完整,因为我没有找到类似 "hashMap.put(groupPosition, ....)" 的代码。 我的意思是您试图获取该组的内容,但是您设置它的地方没有任何代码。仅有的两个 "put()" 方法被称为 "upcoming events" 和 "past events" 的特殊组调用......所以 "hashMap" 只能包含这两个值。