在可扩展列表视图的子视图中设置图像时出现空指针异常
Null pointer exception while setting image in child view of Expandable list view
我在 child.bg[i] = bground[0];
行中得到 Null Pointer Exception
。我正在尝试在子项的 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 中设置可绘制文件夹中的图像。如果还有其他方法,请告诉我。提前致谢。
您未能初始化 ChildItem class 内的 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);
我在 child.bg[i] = bground[0];
行中得到 Null Pointer Exception
。我正在尝试在子项的 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 中设置可绘制文件夹中的图像。如果还有其他方法,请告诉我。提前致谢。
您未能初始化 ChildItem class 内的 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);