1

我正在尝试像这样覆盖 getview 方法:

public class MyArrayAdapter extends ArrayAdapter {
private final Context context;
  private final ArrayList<DataList> values;
  private final  LayoutInflater li = LayoutInflater.from(this.getContext());


  public MyArrayAdapter(Context context, int    textViewResourceId,ArrayList<DataList> values) {
    super(context,textViewResourceId, values);
    this.context = context;
    this.values = values;
  }

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;

        if(convertView==null){
            convertView=li.inflate(R.layout.simplerow,parent,false);
            holder=new ViewHolder();
            holder.text=(TextView)   convertView.findViewById(R.layout.simplerow);
            convertView.setTag(holder);
        }else{
            Log.d("a","entre else");
            holder=(ViewHolder)convertView.getTag();
       }
        DataList b=values.get(1);
        String c=b.toString();
        holder.text.setText(values.get(position).toString());
        try {

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertView;
    }

  static class ViewHolder {
      TextView text;
    }

每次我到达 holder.text.setText(values.get(position).toString())我都会收到null pointer exception错误。VALUES 是arraylistDataList 类型,Datalist 是我的类,它返回两个字符串。我知道 Values 不为空。

4

1 回答 1

3

引发 NullPointerException的原因holder.text是因为您使用了错误的 id:

convertView.findViewById(R.layout.simplerow);

所以findViewById()返回null。尝试类似:

convertView.findViewById(R.id.textView); // I made up a name, but notice the difference?
于 2013-03-06T16:37:51.087 回答