Android ListAdapter显示正确,但随后给出NullPointer。为什么?

时间:2014-01-31 10:25:08

标签: java android nullpointerexception listadapter

我正在尝试在我的Android应用中构建一个简单的listadapter。我得到它正确显示项目列表,但奇怪的是,在列表显示后,我得到一个Nullpointer和应用程序崩溃。看到适配器相当简单,我不知道我在这里做错了什么。

在我的适配器下面。 Nullpointer出现在get holder.points.setText("123");

行的getView方法中
public class QuestionAdapter extends BaseAdapter {

    private List<Question> data = new ArrayList<Question>();
    private Context context;

    public QuestionAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    private class ViewHolder {
        TextView points;
        TextView title;
    }

    public void setList(List<Question> questionList) {
        this.data = questionList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        Question question = getItem(position);

        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_question, null);
            holder.points = (TextView) convertView.findViewById(R.id.text_question_points);
            holder.title = (TextView) convertView.findViewById(R.id.text_question_title);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.points.setText("123");
        holder.title.setText(question.title);

        return convertView;
    }

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

    @Override
    public Question getItem(int i) {
        return data.get(i);
    }
}

4 个答案:

答案 0 :(得分:2)

你在任何地方都没有convertView.setTag(holder)

结果当你做

 holder = (ViewHolder) convertView.getTag();

holder将为null,它会为您提供NullPointerException

答案 1 :(得分:1)

你忘记了

convertView.setTag(holder);

这里

    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_question, null);
        holder.points = (TextView) convertView.findViewById(R.id.text_question_points);
        holder.title = (TextView) convertView.findViewById(R.id.text_question_title);
        convertView.setTag(holder);//this is it
    }

这就是为什么我们第二次使用这个convertView,并且convertView!= null。此行holder中的holder = (ViewHolder) convertView.getTag();为空。

修改 也就是说,虽然我不是调试器。

答案 2 :(得分:0)

public class QuestionAdapter extends BaseAdapter {

    private List<Question> data = new ArrayList<Question>();
    private Context context;

    public QuestionAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    private class ViewHolder {
        TextView points;
        TextView title;
    }

    public void setList(List<Question> questionList) {
        this.data = questionList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        Question question = getItem(position);

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_question, null);
            holder.points = (TextView) convertView.findViewById(R.id.text_question_points);
            holder.title = (TextView) convertView.findViewById(R.id.text_question_title);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.points.setText("123");
        holder.title.setText(question.title);

        return convertView;
    }

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

    @Override
    public Question getItem(int i) {
        return data.get(i);
    }
}

答案 3 :(得分:0)

您尚未调用该函数

 setList(List<Question> questionList)

那里的数据列表不包含任何值。这就是它显示空指针异常的原因。

纠正它,然后检查......