我是NavigationDrawer中ExpandableListView的新手。我的问题是,所有具有子菜单的标题菜单正在扩展和正确折叠,但那些没有任何子菜单的标题菜单会使我的应用程序崩溃并出现NullPointerException。这是我的代码
listDataHeader.add("Home");
listDataHeader.add("Bhopal");
listDataHeader.add("Indore");
listDataHeader.add("Jaipur");
listDataHeader.add("Udaipur");
listDataHeader.add("Ajmer");
listDataHeader.add("Jodhpur");
listDataHeader.add("Chandigarh");
listDataHeader.add("Lakhnow");
// Adding child data
List<String> bhopal_ = new ArrayList<String>();
bhopal_.add("About Us");
bhopal_.add("Board Members");
bhopal_.add("Chaiman Messages");
List<String> indore_ = new ArrayList<String>();
indore_.add("Heritage of Indore");
indore_.add("City Map");
List<String> udaipur_ = new ArrayList<String>();
udaipur_.add("Live");
udaipur_.add("Old");
List<String> lakhnow_ = new ArrayList<String>();
lakhnow_.add("Procurement");
lakhnow_.add("Schemes");
lakhnow_.add("Auctions");
listDataChild.put(listDataHeader.get(1), bhopal_); // Header, Child data
listDataChild.put(listDataHeader.get(2), indore_);
listDataChild.put(listDataHeader.get(4), udaipur_);
listDataChild.put(listDataHeader.get(8), lakhnow_);
这是我的适配器代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Context mContext;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context mContext, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) {
this.mContext = mContext;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return listDataChild.get(listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String header_title = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.sub_menu_list_group, null);
}
TextView tv_head_txt = (TextView) convertView.findViewById(R.id.lblListHeader);
tv_head_txt.setText(header_title);
tv_head_txt.setTypeface(Utilities.myFont(mContext));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String child_title = String.valueOf(getChild(groupPosition, childPosition));
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.menu_list_item, null);
}
TextView tv_head_txt = (TextView) convertView.findViewById(R.id.lblListItem);
tv_head_txt.setText(child_title);
tv_head_txt.setTypeface(Utilities.myFont(mContext));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
在特定标题菜单中没有子菜单时会出现异常。
答案 0 :(得分:1)
您的 groupPosition 即将 null ,这就是它崩溃的原因。你可以在下面的代码中试试这个。
@Override
public int getChildrenCount(int groupPosition) {
int pos =0;
if(groupPosition != null){
if( (listDataHeader.get(groupPosition)!= null) && (listDataChild.get(listDataHeader.get(groupPosition)).size() != null)){
pos = listDataChild.get(listDataHeader.get(groupPosition)).size();
}else{
pos =0;
}
}
return pos;
}
答案 1 :(得分:0)
您只需按如下方式初始化列表,
listDataChild.put(listDataHeader.get(3), new ArrayList<String>());
listDataChild.put(listDataHeader.get(5), new ArrayList<String>());
listDataChild.put(listDataHeader.get(6), new ArrayList<String>());
listDataChild.put(listDataHeader.get(7), new ArrayList<String>());
可以解决您的问题
答案 2 :(得分:0)
将其设为0。
@Override
public int getChildrenCount(int i) {
return listDataChild.get(listDataHeader.get(i)).size()>0?listDataChild.get(listDataHeader.get(i)).size():0;
}