【问题标题】:java.lang.NullPointerException: Attempt to read from field 'android.widget.TextViewjava.lang.NullPointerException:尝试从字段 'android.widget.TextView 读取
【发布时间】:2017-01-31 00:35:10
【问题描述】:

您好,我知道有很多人有类似的问题,但我将其添加为新问题的原因是因为这是一个带有两个支架的适配器。我有一个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;
    }
}

【问题讨论】:

    标签: android listview android-adapter


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。根据我的代码,您建议在哪里进行此修复?
    • @paul590 remove convertView.setTag(holder) from after you switch 并在两个案例陈述
    • 你就是男人!我更多地查看了我的代码,我的问题出在 getTag() 函数上,我得到了一个标签,但是像这样 getTag(R.layout.blah) 这样做并且弄乱了代码,我只是把它拿出来再次运行它奏效了。再次感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多