我在適配器中定義持有者,但每次我試圖引用其元素時,我都會得到NullPointerException
。持有人TextView觸發器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。的
vi = inflater.inflate(R.layout.histlist, null);
Log.d("DeBuG9","DeBuG9");
holder.amountHold = (TextView) findViewById(R.id.hlamount);
我選擇這個作爲答案,因爲這個問題是明確解釋 – Pclaverie