제목 대로 getGroupCount()와 getChildrenCount() 함수에서
카운트 할 그룹이나 자식이 없는 경우에 널포인터익셉션이 나고 있습니다.ㅠㅠ
동적으로 그룹과 자식을 넣는 뷰를 만들고 있는데 도움 부탁 드립니다.
@Override
public int getChildrenCount(int groupPosition) {
if(children.get(groupPosition).size() == 0)
{
return 0;
}
return children.get(groupPosition).size();
}
@Override
public String getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
if (groups.size() == 0) {
return ;
}
return groups.size();
}
kfmes 님 감사합니다! 그런데 아무리 해봐도 잘 되지가 않네요;;ㅠㅠㅠㅠ
밑에 코드가 xml을 제외한 모든 코드입니다. 혹시라도 보시고 잘못된 부분 있으면 지적 부탁드리겠습니다 ㅠ
public class ZzzExpanTestActivity extends Activity {
private ArrayList<String> groups;
private ArrayList<ArrayList<ArrayList<String>>> childs;
ImageButton HomeBtn, AddBtn;
myExpandableAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window win = getWindow();
win.requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
final ExpandableListView l = (ExpandableListView) findViewById(R.id.ExpandableListView01);
groups = new ArrayList<String>();
//childs = new ArrayList<ArrayList<ArrayList<String>>>();
//loadData();
adapter = new myExpandableAdapter(this, groups, childs);
l.setAdapter(adapter);
l.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,
final int groupPosition, long id) {
return false;
}
});
l.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
final int groupPosition, final int childPosition, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(
ZzzExpanTestActivity.this);
builder.setTitle("세부 항목 삭제")
.setPositiveButton("삭제",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
childs.remove(adapter.getChild(groupPosition, childPosition));
adapter.notifyDataSetChanged();
}
}).setNegativeButton("취소", null).show();
return false;
}
});
}
public class myExpandableAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groups;
private ArrayList<ArrayList<ArrayList<String>>> children;
private Context context;
public myExpandableAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<ArrayList<String>>> children) {
this.context = context;
this.groups = groups;
this.children = childs;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public ArrayList<String> getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String child = (String) ((ArrayList<String>) getChild(
groupPosition, childPosition)).get(0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.expandablelistview_child, null);
}
TextView childtxt = (TextView) convertView
.findViewById(R.id.TextViewChild01);
childtxt.setText(child);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if(children.get(groupPosition).equals(null))
return 0;
else
return children.get(groupPosition).size();
}
@Override
public String getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
if (groups.equals(null)) {
return 0;
}
else
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.expandablelistview_group, null);
}
TextView grouptxt = (TextView) convertView
.findViewById(R.id.TextViewGroup);
grouptxt.setText(group);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
private void loadData() {
groups.add("Group 1");
groups.add("Group 2");
groups.add("Group 3");
childs.add(new ArrayList<ArrayList<String>>());
childs.get(0).add(new ArrayList<String>());
childs.get(0).get(0).add("Child 1 group 1");
childs.get(0).add(new ArrayList<String>());
childs.get(0).get(1).add("Child 2 group 1");
childs.get(0).add(new ArrayList<String>());
childs.get(0).get(2).add("Child 3 group 1");
childs.add(new ArrayList<ArrayList<String>>());
childs.get(1).add(new ArrayList<String>());
childs.get(1).get(0).add("Child 1 group 2");
childs.get(1).add(new ArrayList<String>());
childs.get(1).get(1).add("Child 2 group 2");
childs.get(1).add(new ArrayList<String>());
childs.get(1).get(2).add("Child 3 group 2");
childs.add(new ArrayList<ArrayList<String>>());
childs.get(2).add(new ArrayList<String>());
childs.get(2).get(0).add("Child 1 group 3");
childs.get(2).add(new ArrayList<String>());
childs.get(2).get(1).add("Child 2 group 3");
childs.get(2).add(new ArrayList<String>());
childs.get(2).get(2).add("Child 3 group 3");
}
public void AddClick(View v) {
switch (v.getId()) {
case R.id.CheckListAddBtn:
LayoutInflater inf1 = (LayoutInflater) getApplicationContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inf1.inflate(R.layout.addfolder, null);
AlertDialog.Builder builder = new AlertDialog.Builder(
ZzzExpanTestActivity.this);
builder.setView(layout)
.setTitle("폴더 추가")
.setPositiveButton("추가",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
EditText edit = ((EditText) ((AlertDialog) dialog)
.findViewById(R.id.addFolder));
String addFolder = edit.getText()
.toString();
if (edit.getText().toString().length() != 0) {
groups.add(addFolder);
adapter.notifyDataSetChanged();
}
}
}).setNegativeButton("취소", null).show();
break;
case R.id.AlarmHome:
startActivity(new Intent(ZzzExpanTestActivity.this, test.class));
break;
}
}
}




해당 그룹이나 자식이 없는경우에도 (null 인경우에) 0을 리턴하게 하면 됩니다.