NullpointerException覆盖arrayadapter中的getview

时间:2013-03-06 16:36:37

标签: android

我正在尝试覆盖这样的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是DataList类型的arraylist,Datalist是我的类,返回两个字符串。我知道值不是空的。

1 个答案:

答案 0 :(得分:3)

holder.text抛出NullPointerException的原因是因为你使用了错误的id:

convertView.findViewById(R.layout.simplerow);

因此findViewById()会返回null。尝试类似:

convertView.findViewById(R.id.textView); // I made up a name, but notice the difference?