ArrayAdapter - ViewHolder空指针错误

时间:2016-03-28 07:03:32

标签: java android adapter android-viewholder

我以为我已经掌握了如何使用ViewHolders但是我现在在运行它时遇到了一个空指针异常。它发生在我尝试为其中一个itemTitleView设置文本时,我不太明白我错过了什么。我有三种使用不同背景颜色的布局,并且我试图根据ArrayList的特定元素的listViewItemType使用每种布局。我有一个谷歌,但对我来说并不是更清楚,任何帮助都会受到赞赏。

PostItemAdapter.java

public class PostItemAdapter extends ArrayAdapter<Item>
{
    private LayoutInflater inflater;
    private ArrayList<Item> datas;
    public static final int TYPE_short = 0;
    public static final int TYPE_med = 1;
    public static final int TYPE_long = 2;

    public PostItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects)
    {
        super(context, textViewResourceId, objects);

        inflater = ((Activity) context).getLayoutInflater();
        datas = objects;
    }

    //class to hold view object references
    static class ViewHolder
    {
        TextView itemTitleView;
        TextView itemTitleView1;
        TextView itemTitleView2;
        ImageView itemThumbView;
    }

    @Override
    public int getItemViewType(int position) {
        return datas.get(position).viewtype;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        int listViewItemType = getItemViewType(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.postitem, null);

            viewHolder = new ViewHolder();
            viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);

            if (listViewItemType == TYPE_short) {
                viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
            } else if (listViewItemType == TYPE_med) {
                viewHolder.itemTitleView1 = (TextView) convertView.findViewById(R.id.itemTitleLabel1);
            } else if (listViewItemType == TYPE_long) {
                viewHolder.itemTitleView2 = (TextView) convertView.findViewById(R.id.itemTitleLabel2);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (datas.get(position).itemThumbUrl == null)
        {
            viewHolder.itemThumbView.setImageResource(R.drawable.test);
        }

        viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
        viewHolder.itemTitleView1.setText(datas.get(position).itemTitle);
        viewHolder.itemTitleView2.setText(datas.get(position).itemTitle);

        return convertView;
    }
}

2 个答案:

答案 0 :(得分:1)

在将标题设置为if else时,您需要添加TextView语句。如果viewHolder.itemTitleView不等于null,则listViewItemType将为TYPE_short

if (listViewItemType == TYPE_short) {
  viewHolder.itemTitleView.setText(datas.get(position).itemTitle);      
} else if (listViewItemType == TYPE_med) {
  viewHolder.itemTitleView1.setText(datas.get(position).itemTitle);      
} else if (listViewItemType == TYPE_long) {
  viewHolder.itemTitleView2.setText(datas.get(position).itemTitle);      
}  

答案 1 :(得分:1)

您的NullPointer exception来了,因为您要查看itemTitleViewitemTitleView1itemTitleView2,其中只有一个是初始化的。您必须将if else状态与初始化变量时相同,但将数据设置为textViews时。

if (listViewItemType == TYPE_short) {
  viewHolder.itemTitleView.setText(datas.get(position).itemTitle);      
}

依旧......