當試圖將圖像填充到持有人的ImageView中或當我嘗試將持有人圖像視圖設置爲onclick偵聽器時,該應用崩潰。它實際上崩潰,如果我試圖做什麼與持有人的imageview相關。Android ListView適配器返回NullPointerException
圖片URL不爲空,我查
的ImageView的存在,我查
適配器代碼(它的重要位):
public class FeedAdapter extends BaseAdapter {
static private Activity activity;
private static LayoutInflater inflater = null;
ArrayList<ActivityTable> actList = new ArrayList<ActivityTable>();
Holder holder;
public FeedAdapter(Activity a, ArrayList<ActivityTable> actList) {
activity = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.actList = actList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ActivityTable act = actList.get(position);
if (vi == null) {
vi = inflater.inflate(R.layout.feed_single_picture, parent, false);
holder = new Holder();
holder.media = (ImageView) vi.findViewById(R.id.postphoto);
//can't set an image to it either. it crashes.
}
else {
holder = (Holder) vi.getTag();
}
//This is where it crashes
holder.media.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//other on Click related Code
}
});
return vi;
}
public static class Holder {
ImageView media;
}
}
爲什麼Holder的ImageView崩潰了嗎?我嘗試添加一個TextView,ImageButton等,所有這些都正常工作。只是這個特定的imageview崩潰。我究竟做錯了什麼?
編輯:崩潰日誌
FATAL EXCEPTION: main
Process: com.metalproject.metalprojectmobile, PID: 5737
java.lang.NullPointerException
at Adapter.FeedAdapter.getView(FeedAdapter.java:456)
at android.widget.AbsListView.obtainView(AbsListView.java:2689)
at android.widget.ListView.makeAndAddView(ListView.java:1801)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillGap(ListView.java:661)
以上線:456分 - > holder.media.setOnClickListener(新View.OnClickListener(){
顯示崩潰日誌 –
肯定。堅持@ρяσѕρєяK – Dinuka
@ρяσѕρєяK編輯 – Dinuka