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

时间:2018-07-15 06:26:22

标签: java android expandablelistview expandablelistadapter

它表明问题出在这里:

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

我尝试打印地图大小,但确实如此。它只有两个键:“即将发生的事件”和“过去的事件”。我使用containsKey()进行了检查,结果为true。但是我仍然无法以某种方式获取List对象的大小。

这可能有助于了解我没有为此创建另一个类文件,并且所有代码都捆绑在同一个类文件中。

这是我的代码:

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) {

        }
    });

1 个答案:

答案 0 :(得分:0)

也许您在此处输入的代码不完整,因为我找不到“ hashMap.put(groupPosition,....)”之类的东西。 我的意思是,您试图获取该组的内容,但是没有任何代码可以对其进行设置。对于称为“即将发生的事件”和“过去的事件”的特殊组,仅调用两个“ put()”方法。.....因此,“ hashMap”只能包含这两个值。