Android 如果ListView为空,为什么会崩溃?
在我的应用程序中,我有一个列表视图:Android 如果ListView为空,为什么会崩溃?,android,listview,expandablelistview,Android,Listview,Expandablelistview,在我的应用程序中,我有一个列表视图: lv = (ExpandableListView) v.findViewById(R.id.listOfShifts); 当我设置适配器时: adapter = new ExpandableListCustom(getActivity(), fullList,temp); lv.setAdapter(adapter); 它崩溃了,但如果完整列表和临时值不为空,则工作正常。。 如何克服崩溃,如果列表为空,屏幕上没有任何内容,也没有崩溃 //Expanda
lv = (ExpandableListView) v.findViewById(R.id.listOfShifts);
当我设置适配器时:
adapter = new ExpandableListCustom(getActivity(), fullList,temp);
lv.setAdapter(adapter);
它崩溃了,但如果完整列表和临时值不为空,则工作正常。。
如何克服崩溃,如果列表为空,屏幕上没有任何内容,也没有崩溃
//ExpandableListCustom.class:
private Context context;
private HashMap<ArrayList<ClockModel>,ArrayList<ClockModel> > fullList; //Headers
ArrayList<ClockModel> child; //data
public ExpandableListCustom(Context context,
HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>> fullList,
ArrayList<ClockModel> child) {
super();
this.context = context;
this.fullList = fullList;
this.child = child;
clockSet = new Clock();
}
它在这里崩溃:
@Override
public int getGroupCount() {
if(fullList != null){
return this.fullList.get(child).size();
}else{
return 0;
}
}
检查这些列表的空值后设置适配器
if(fullList != null && temp != null) {
adapter = new ExpandableListCustom(getActivity(), fullList,temp);
lv.setAdapter(adapter);
}
如果要显示空列表,请在onCreateView中创建fullList和temp对象。。。设置适配器之前。发布错误并发布适配器指示行197@ExpandableListCustom?你们在活动类上初始化列表了吗?是的..然后它就可以工作了,但我想显示空列表,你们可以像fullList=newarrayli一样初始化。。。并且temp=new ArrayL….,如果你不放任何东西在上面,你可以有一个空的可扩展列表为什么你在getGroupCount中使用child?您需要更改并返回this.fullList.getchild.size;返回此.fullList.size;谢谢,但我有一个微调器控制列表上的数据。有时列表为空,有时已满..更改return fullList.size;
@Override
public int getGroupCount() {
if(fullList != null){
return this.fullList.get(child).size();
}else{
return 0;
}
}
if(fullList != null && temp != null) {
adapter = new ExpandableListCustom(getActivity(), fullList,temp);
lv.setAdapter(adapter);
}