BaseExpandableListAdapter为复选框抛出NPE

时间:2013-01-16 15:25:13

标签: android checkbox nullpointerexception

我有一个public class BaseFilterAdapter extends BaseExpandableListAdapter implements OnCheckedChangeListener,我正在按

设置组和子复选框ID
public BaseFilterAdapter(Context context, ExpandableListView topExpList) {
    this.context = context;
    this.topExpList = topExpList;
    inflater = LayoutInflater.from(context);
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View chajld, ViewGroup parent) {

    if (chajld == null)
        chajld = inflater.inflate(R.layout.elv_child, parent, false);


    CheckBox cb = (CheckBox) chajld.findViewWithTag("chckb");
    cb.setId(ID_MAX_CHK * (groupPosition + 1) + childPosition + 1);
    cb.setOnCheckedChangeListener(this);
    cb.setFocusable(false);

    return chajld;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View grup,
        ViewGroup parent) {
    if (grup == null)
        grup = inflater.inflate(R.layout.elv_parent, parent, false);

    CheckBox cb = (CheckBox) grup.findViewWithTag("chckb");
    cb.setId(ID_MAX_CHK * (groupPosition + 1));
    cb.setOnCheckedChangeListener(this);
    cb.setFocusable(false);

    return grup;
}

并且在同一个类中我重写了OnCheckedChangeListener

@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
    cb.setFocusable(false);
    int groupPos = (int) (cb.getId() / ID_MAX_CHK) - 1;
    if (cb.getId() % ID_MAX_CHK == 0) {
        ...
    } else {
        int childPos = (int) (cb.getId() % ID_MAX_CHK) - 1;

        CheckBox cbb = (CheckBox) topExpList.findViewById(ID_MAX_CHK
                * (groupPos + 1));

          ....
    }
}

在我发布的最后一行投掷NPE。当我在子视图中更改复选框的isChecked时调用它。

任何人都可以注意到为什么会这样吗? 感谢

(PS。我删除了所有不必要的行)

编辑: topExpListExpandableListView,设置为此适配器。

编辑:来自SuperMap extends MapActivity班级:

private void inflateFilterMenu() {
    filterMenu = (ExpandableListView) findViewById(R.id.lstFilter);
    bfa = new BaseFilterAdapter(getBaseContext(), filterMenu);
    filterMenu.setAdapter(bfa);
    for (int j = 0; j < bfa.getGroupCount(); j++) {
        filterMenu.expandGroup(j);
        filterMenu.collapseGroup(j);
    }
    filterMenu.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            return bfa.onChildClick(parent, v, groupPosition,
                    childPosition, id);
        }

    });
    filterMenu.setVisibility(View.VISIBLE);
}

0 个答案:

没有答案