我用部分建立一个listview.我使用的是this帖子的答案,之前曾问过一个问题,但又被卡住了.我认为这是一个非常奇怪的错误.
当我开始活动时,我可以在屏幕上看到列表,就像我想要的一样.但是,当我尝试开始滚动活动时就崩溃了.我以为我以相同的方式实施了所有操作,但显然不是.
我的适配器:
public class DelftAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SECTION = 1;
private Activity activity;
private List<ListItem> listItems;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private final int[] bgColors = new int[] { R.color.list_odd, R.color.list_even };
public DelftAdapter(Activity a, ArrayList<ListItem> li) {
activity = a;
listItems = li;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return listItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return listItems.get(position).isSection() ? TYPE_SECTION : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2; // sectionheader and regular item
}
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View vi=convertView;
final ListItem li = listItems.get(position);
ItemViewHolder itemHolder;
SectionViewHolder sectionHolder;
switch (type) {
case TYPE_SECTION: // is sectionheader
if (vi == null) { //convertview==null
sectionHolder = new SectionViewHolder();
vi = inflater.inflate(R.layout.sectionedlistitem, null);
vi.setOnClickListener(null);
vi.setOnLongClickListener(null);
vi.setLongClickable(false);
sectionHolder.title = (TextView) vi.findViewById(R.id.list_header_title);
}else{//convertview is not null
sectionHolder = (SectionViewHolder)vi.getTag();
}
SectionItem si = (SectionItem)li;
sectionHolder.title.setText(si.getTitle());
break;
case TYPE_ITEM:// no sectionheader
if (vi == null) { //convertview==null
itemHolder = new ItemViewHolder();
vi = inflater.inflate(R.layout.singlelistitem, null);
itemHolder.name=(TextView)vi.findViewById(R.id.tvname);
itemHolder.tip=(TextView)vi.findViewById(R.id.tvtip);
itemHolder.image=(ImageView)vi.findViewById(R.id.image);
}else{ // convertview != null
itemHolder = (ItemViewHolder)vi.getTag();
}
ListData ld = (ListData)li;
itemHolder.name.setText(ld.name);
itemHolder.tip.setText(ld.tip);
if (ld.photoUrl != null ){
imageLoader.DisplayImage(ld.photoUrl, itemHolder.image);
}else{
itemHolder.image.setImageURI(Uri.fromFile(new File("//assets/nopic.png")));
}
// alternating colors
int colorPos = position % bgColors.length;
vi.setBackgroundResource(bgColors[colorPos]);
break;
}
return vi;
}
public static class SectionViewHolder {
public TextView title;
}
public static class ItemViewHolder {
public TextView name;
public TextView tip;
public ImageView image;
}
}
我为两种不同的视图构建了两个ViewHolders.发生的错误是itemHolder.name.setText(ld.name);上的NullPointerException;线.
我没有得到的是该代码适用于前几个条目,但是当我开始滚动时却失败了.在我正在使用的数据中,名称和提示永远不会为空,只有photoUrl可能是但代码中已涵盖.
有人知道为什么这段代码会失败吗?
解决方法:
在创建新的视图保持器并为新视图充气的代码路径中,实际上从未将viewHolder存储在Views标记中,因此,当滚动并获取一个exisitng视图时,view.gettag()返回null,稍后再尝试时并使用ViewHolder获得Null指针异常.您需要将调用添加到setTag().