The app crashes when trying to populate images in to a holder's ImageView or when I try to set an onclick listener to the holder imageview. It actually crashes if I try to do anything related to the holder's imageview.
The Image URL is not null, I checked
The ImageView exists, I checked
Adapter code (Important bits of it):
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;
}
}
Why does the Holder's ImageView crash? I tried adding a TextView, ImageButton, etc and all of them work properly. Just this specific imageview crashes. What am I doing wrong?
EDIT: Crash Log
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)
The above line: 456 points to -> holder.media.setOnClickListener(new View.OnClickListener() {
you should modify your code as below :
if (vi == null) {
vi = inflater.inflate(R.layout.feed_single_picture, parent, false);
holder = new Holder();
holder.media = (ImageView) vi.findViewById(R.id.postphoto);
}
and also remove them from outer code of if else
Fix will be like this:
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();
vi.setTag(holder);
}else {
holder = (Holder) vi.getTag();
}
holder.media.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return vi;
}
static class Holder {
private ImageView media;
Holder(View view){
this.media = (ImageView) view.findViewById(R.id.postphoto);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.