我在第Null Pointer Exception
行获得child.bg[i] = bground[0];
。我正在尝试在子项的ImageView
中设置图像。我在子项目布局中有ImageView
和TextView
。以下是Expandable List View
中填充数据的代码。
List<GroupItem> items = new ArrayList<GroupItem>();
int[] bground ={R.drawable.hawamahal,R.drawable.tajmahal,R.drawable.mysorepalace};
// Populate our list with groups and it's children
for(int i = 0; i < 3; i++) {
GroupItem item = new GroupItem();
switch(i){
case 0: item.title = "Western India";
{
ChildItem child = new ChildItem();
child.title = getString(R.string.WestDescription);
child.bg[i] = bground[0];
item.items.add(child);
}
break;
case 1: item.title = "Northern India";
{
ChildItem child = new ChildItem();
child.title = getString(R.string.NorthDescription);
child.bg[i] = bground[1];
item.items.add(child);
}
break;
case 2: item.title = "Southern India";
{
ChildItem child = new ChildItem();
child.title = getString(R.string.SouthDescription);
child.bg[i] = bground[2];
item.items.add(child);
}
break;
}
items.add(item);
}
`
适配器和持有人的代码
private static class GroupItem {
String title;
List<ChildItem> items = new ArrayList<ChildItem>();
}
private static class ChildItem {
String title;
int[] bg;
}
private static class ChildHolder {
TextView title;
ImageView childimage;
}
private static class GroupHolder {
TextView title;
}
/**
* Adapter for our list of {@link GroupItem}s.
*/
private class ExampleAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
private LayoutInflater inflater;
private List<GroupItem> items;
public ExampleAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public void setData(List<GroupItem> items) {
this.items = items;
}
@Override
public ChildItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).items.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
int[] bg={R.drawable.taj};
ChildItem item = getChild(groupPosition, childPosition);
if (convertView == null) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
holder.childimage= (ImageView) convertView.findViewById(R.id.ChildImageView);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
holder.title.setText(item.title);
holder.childimage.setImageResource(bg[groupPosition]);
return convertView;
}
@Override
public int getRealChildrenCount(int groupPosition) {
return items.get(groupPosition).items.size();
}
@Override
public GroupItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
@Override
public int getGroupCount() {
return items.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
GroupItem item = getGroup(groupPosition);
if (convertView == null) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.group_item, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.title.setText(item.title);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
我是Android开发的新手,如果您需要任何进一步的细节,请告诉我。这里我试图在ChildItem的ImageView中从drawable文件夹设置图像。如果还有其他方法,请告诉我。在此先感谢。
答案 0 :(得分:0)
您未能在ChildItem类中初始化child.bg []数组 按如下方式更改您的代码:
ChildItem child = new ChildItem();
child.title = getString(R.string.WestDescription);
child.bg[] = new int[3]; // <----- initiliaze bg[] arrays
child.bg[i] = bground[0];
item.items.add(child);