2014-01-31 74 views
0

我想在我的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); 
    } 
} 
+0

確實R.id.text_question_points屬於list_item_question? – Blackbelt

回答

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

你沒有在任何地方做convertView.setTag(holder)

當你做

holder = (ViewHolder) convertView.getTag(); 

holder將是無效,它會給你一個NullPointerException

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); 
    } 
} 
0

您還沒有調用的函數

setList(List<Question> questionList) 

有結果數據列表不包含任何值。這就是爲什麼它顯示空指針異常。

糾正它,並檢查....

相關問題