ExpandableListView:尝试调用接口方法';int java.util.List.size()';关于空对象引用
这表明问题在这里:ExpandableListView:尝试调用接口方法';int java.util.List.size()';关于空对象引用,java,android,expandablelistview,expandablelistadapter,Java,Android,Expandablelistview,Expandablelistadapter,这表明问题在这里: @Override public int getChildrenCount(int groupPosition) { return hashMap.get(groupPosition).size(); } 我试着打印地图的大小,结果它打印出来了。它只有两个键:“即将到来的事件”和“过去的事件”。我使用containsKey()进行了检查,结果为true。但不知何故,我仍然无法获得作为值包含的列表对象的大小 知道我没有为此创建另一个类文件可能会有所帮助,所有代码都捆绑
@Override
public int getChildrenCount(int groupPosition) {
return hashMap.get(groupPosition).size();
}
我试着打印地图的大小,结果它打印出来了。它只有两个键:“即将到来的事件”和“过去的事件”。我使用containsKey()进行了检查,结果为true。但不知何故,我仍然无法获得作为值包含的列表对象的大小
知道我没有为此创建另一个类文件可能会有所帮助,所有代码都捆绑在同一个类文件中
这是我的密码:
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;
}
}
class CustomEventListAdapter扩展了BaseExpandableListAdapter{
HashMap;
上下文con;
CustomEventListAdapter(上下文上下文,HashMap){
hashMap=map;
con=上下文;
}
List groupTitle=Arrays.asList(“即将发生的事件”、“过去的事件”);
@凌驾
public int getGroupCount(){
返回groupTitle.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回hashMap.get(groupPosition.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回groupTitle.get(groupPosition);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回hashMap.get(groupTitle.get(groupPosition)).get(childPosition);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
if(convertView==null){
LayoutFlater LayoutFlater=(LayoutFlater)getApplicationContext()
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.expandable\u list\u组,空);
}
TextView listTitleTextView=(TextView)convertView
.findviewbyd(R.id.groupHeader);
listTitleTextView.setTypeface(null,Typeface.BOLD);
listTitleTextView.setText(groupTitle.get(groupPosition));
返回convertView;}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
if(convertView==null){
LayoutFlater LayoutFlater=(LayoutFlater)getApplicationContext()
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.expandable\u list\u项,空);
}
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());
返回convertView;}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}
这是onCreate():
expandableListView=(expandableListView)findViewById(R.id.eventList);
hashMap=新的hashMap();
FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference=database.getReference().child(“事件”);
reference.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
List eventList=new ArrayList();
//List uncoming=new ArrayList();
//列表过去=新的ArrayList();
对于(DataSnapshot postSnapshot:DataSnapshot.getChildren()){
事件=新事件();
event.name=String.valueOf(postsnashot.child(“name”).getValue());
event.details=String.valueOf(postsnashot.child(“details”).getValue());
event.add=String.valueOf(postsnashot.child(“PhotoAdd”).getValue());
event.date=String.valueOf(postsnashot.child(“日期”).getValue());
添加(事件);
Toast.makeText(EventsActivity.this,“longDate=“+event.getLongDate(),Toast.LENGTH_SHORT).show();
}
long current=Calendar.getInstance().getTimeInMillis();
sort(eventList,new Sortbyroll());
int i=0;
对于(i=0;i
可能您在此处输入的代码不完整,因为我没有找到类似“hashMap.put(groupPosition,…)”的内容。
我的意思是,你试图得到该组的内容,但没有任何代码,你设置它。只有两个“put()”方法被称为“即将到来的事件”和“过去的事件”的特殊组调用……因此“hashMap”只能包含这两个值。请提供创建
hashMap
map@Lino请再看一看。像@lexicore No先生的复制品一样关闭,
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) {
}
});