java.lang.NullPointerException:尝试从字段中读取' android.widget.TextView

时间:2017-01-31 00:35:11

标签: android listview android-adapter

您好我意识到有多个人有类似的问题,但我将其添加为新问题的原因是因为这是一个有两个持有人的适配器。我有一个listview,它有一个标题单元格和信息单元格。我遇到的错误

  

java.lang.NullPointerException:尝试从字段' android.widget.TextView中读取。

只有当我向下滚动列表然后再向上时才会发生这种情况。我假设holder被破坏,当我调用该行添加文本时,持有人为空但我不确定。感谢您的帮助!

自定义适配器:

private class MyCustomAdapter extends ArrayAdapter<sources> {

    private ArrayList<sources> sources_list = new ArrayList<sources>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private LayoutInflater vi;

    public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<sources> sources_list) {
        super(context, textViewResourceId, sources_list);
        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final sources item) {
        sources_list.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final sources item) {
        sources_list.add(item);
        sectionHeader.add(sources_list.size() - 1);
        notifyDataSetChanged();
    }

    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    public int getCount() {
        return sources_list.size();
    }

    public int getViewTypeCount() {
        return 2;
    }

    public sources getItem(int position) {
        return sources_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class ViewHolder {
        TextView code;
        CheckBox name;
    }

    public class ViewHolder2 {
        TextView separator;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ViewHolder2 holder2 = null;
        Log.v("ConvertView", String.valueOf(position));
        int rowType = getItemViewType(position);
        if (convertView == null) {
            switch (rowType) {
                case TYPE_ITEM:
                    convertView = vi.inflate(R.layout.source_cell, null);
                    holder = new ViewHolder();
                    holder.code = (TextView)
                            convertView.findViewById(R.id.code);
                    holder.name = (CheckBox)
                            convertView.findViewById(R.id.checkBox1);
                    break;

                case TYPE_SEPARATOR:
                    holder2 = new ViewHolder2();
                    convertView = vi.inflate(R.layout.source_header, null);
                    holder2.separator = (TextView)
                            convertView.findViewById(R.id.separator);
                    break;
            }

            convertView.setTag(holder);

        } else {
            if (rowType == TYPE_ITEM) {
                holder = (ViewHolder)
                        convertView.getTag(R.layout.source_cell);
            } else {
                holder2 = (ViewHolder2) convertView.getTag(R.layout.source_header);
            }

        }

        if (rowType == TYPE_ITEM) {
            sources n_source = sources_list.get(position);
            holder.code.setText(n_source.getCode().toUpperCase());
            holder.name.setTag(n_source);
        } else {
            sources n_source = sources_list.get(position);
            holder2.separator.setText(n_source.getCode().toUpperCase());
        }
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:2)

您的setTag/getTag似乎有误。您需要使用setTag(r.layout.blah, holder)来匹配getTag(r.layout.blah)