没有 Child 项的可扩展列表视图

Expandable listview without Child Item

我在创建应用程序时使用可扩展列表视图问题是当 parent 没有选择 child 时应用程序崩溃并出现空指针异常(就像我的代码中的第一个最后一个列表 header 所有其他 header 中都有 child,在选择时会向其片段移动)。如何处理这个问题??

提前谢谢你下面是我的示例代码:

主要代码 activity

expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            v.setSelected(true);
            switch (groupPosition) {
                case 0:
                    fragment = new HomeFragment();
                    break;
                case 1:
                    switch (childPosition) {
                        case 0:
                            fragment = new PrimerFragment();
                            break;
                        case 1:
                            fragment = new SettingTabFragment();
                            break;
                        default:
                            break;
                    }
                    break;

                    case 2:
                    switch (childPosition) {
                        case 0:
                            fragment = new TabFragment();
                            break;
                        case 1:
                            fragment = new CreamTabFragment();
                            break;
                        case 2:
                            fragment = new FoundationFragment();
                            break;
                        default:
                            break;
                    }
                    break;
                case 3:
                    finish();
                    break;
                default:
                    fragment=new HomeFragment();
                    break;
            }

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.content_frame, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
            mDrawerLayout.closeDrawer(expandableList);
            return false;
        }
    });
}


  listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    String[] array = getResources().getStringArray(R.array.nav_drawer_items);
    listDataHeader = Arrays.asList(array);

    // setting essentials

    List<String> setting = new ArrayList<String>();
    String[] set = getResources().getStringArray(R.array.setting);
    setting = Arrays.asList(set);

    // foundation essentials

    List<String> foundation = new ArrayList<String>();
    String[] found = getResources().getStringArray(R.array.foundation);
    foundation = Arrays.asList(found);

    listDataChild.put(listDataHeader.get(0), new ArrayList<String>()); 
    listDataChild.put(listDataHeader.get(1), setting);
    listDataChild.put(listDataHeader.get(2), foundation);
    listDataChild.put(listDataHeader.get(6), new ArrayList<String>());
}

可扩展列表视图适配器代码

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context,
                             List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@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 View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition,
            childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_items, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.listItems);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(
            this._listDataHeader.get(groupPosition)).size();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_header, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.listheader);
    // lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    return convertView;
}
@Override
public boolean hasStableIds() {
    return false;
}

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

你知道空指针异常是在什么地方抛出的吗?

也许在这里:

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(
            this._listDataHeader.get(groupPosition)).size();
}

您在空引用中调用 .size()。你试过了吗?

@Override
public int getChildrenCount(int groupPosition) {
    if (this._listDataChild.get(
               this._listDataHeader.get(groupPosition)) == null) {
       return 0;

    } else {
       return this._listDataChild.get(
            this._listDataHeader.get(groupPosition)).size();
    }
}