我想在我的Android應用程序中構建一個簡單的listadapter。我知道它顯示正確的項目列表,但奇怪的是,在列表顯示後,我得到一個Nullpointer,應用程序崩潰。看到適配器很簡單,我不知道我在這裏做錯了什麼。Android ListAdapter顯示正確,但會給出NullPointer。爲什麼?
在我的適配器下面。該空指針在getView方法在該行稱holder.points.setText("123");
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);
}
}
確實R.id.text_question_points屬於list_item_question? – Blackbelt