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

时间:2018-07-30 19:32:41

标签: android expandablelistview

我在创建应用程序的地方使用可扩展列表视图,问题是当选择父级没有孩子时,应用程序崩溃并出现空指针异常(例如从我的代码中,它的第一个和最后一个列表标头所有其他标头都有该子标头)在选择过程中,它们会走向碎片。该如何处理?

在此先感谢您的示例代码:

主要活动的代码

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;
}

1 个答案:

答案 0 :(得分:0)

您知道在哪里抛出空指针异常吗?

也许在这里:

@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();
    }
}