holder TextView触发器nullPointerException

时间:2014-01-23 12:02:12

标签: android android-listview nullpointerexception adapter

我在适配器中定义了一个持有者,但每当我尝试引用它的元素时,我得到一个NullPointerException

这是适配器:

private class StableArrayAdapter extends ArrayAdapter<event> {
    private Context context;
    private List<event> mylist;

    public StableArrayAdapter(Context context, List<event> mylist) {
        super(context, R.layout.histlist, mylist);
        this.context = context;
        this.mylist = mylist;
    }

    public class ViewHolder {
        public TextView amountHold;
        public TextView typeHold;
        public TextView DateHold;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) {
            final ViewHolder holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi = inflater.inflate(R.layout.histlist, null);
            Log.d("DeBuG9","DeBuG9");
            holder.amountHold = (TextView) findViewById(R.id.hlamount);
Log.d("DeBuG10","DeBuG10"+holder.amountHold.getText().toString());
            holder.DateHold = (TextView) findViewById(R.id.hldate);
            holder.typeHold = (TextView) findViewById(R.id.hltype);
            vi.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) vi.getTag();
        final event e = mylist.get(position);
        Log.d("DeBuG","DeBuG713"+e.getAmount().toString()+e.getDate()+e.getType());
        holder.amountHold.setText(e.getAmount().toString()+"€");
        holder.DateHold.setText(e.getDate());
        holder.typeHold.setText(e.getType());
        return vi;
    }
}

这是我列表的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView 
    android:id="@+id/hltype"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:text="f"
    android:layout_height="wrap_content"/>
<TextView
    android:id="@+id/hldate"
    android:layout_weight="1" 
    android:text="f"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
<TextView 
    android:id="@+id/hlamount"
    android:layout_weight="1"
    android:text="f"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

我注意到每次我试图引用holder.amoutHold(就像DEBUG10一样)我得到一个NPE。

4 个答案:

答案 0 :(得分:3)

使用此

        vi = inflater.inflate(R.layout.histlist, null);
        Log.d("DeBuG9","DeBuG9");
        holder.amountHold = (TextView)vi.findViewById(R.id.hlamount); // this is main 

而不是

       vi = inflater.inflate(R.layout.histlist, null);
        Log.d("DeBuG9","DeBuG9");
        holder.amountHold = (TextView) findViewById(R.id.hlamount);

答案 1 :(得分:2)

您必须使用您充气的视图来检索其内容。所以你必须写 对于属于vi.findViewById

的外部视图,histlist

答案 2 :(得分:1)

// try to replcae this peace of code and let me know still have stuff.

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi = inflater.inflate(R.layout.histlist, null,false);
            holder.amountHold = (TextView) findViewById(R.id.hlamount);
            holder.DateHold = (TextView) findViewById(R.id.hldate);
            holder.typeHold = (TextView) findViewById(R.id.hltype);
            vi.setTag(holder);
        }else{
            holder = (ViewHolder) vi.getTag();
        }
        final event e = mylist.get(position);
        holder.amountHold.setText(e.getAmount().toString()+"€");
        holder.DateHold.setText(e.getDate());
        holder.typeHold.setText(e.getType());
        return vi;
}

答案 3 :(得分:1)

在你的getview方法中我添加更改plz检查这个我觉得有用。

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

            if (convertView == null) {
                 holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.histlist, null);
                Log.d("DeBuG9","DeBuG9");
                holder.amountHold = (TextView) convertView.findViewById(R.id.hlamount);
    Log.d("DeBuG10","DeBuG10"+holder.amountHold.getText().toString());
                holder.DateHold = (TextView) convertView.findViewById(R.id.hldate);
                holder.typeHold = (TextView) convertView.findViewById(R.id.hltype);
                vi.setTag(holder);
            }else{
            holder = (ViewHolder) convertView.getTag();
            }final event e = mylist.get(position);
            Log.d("DeBuG","DeBuG713"+e.getAmount().toString()+e.getDate()+e.getType());
            holder.amountHold.setText(e.getAmount().toString()+"€");
            holder.DateHold.setText(e.getDate());
            holder.typeHold.setText(e.getType());
            return convertView;
        }