I made an adapter for a ListView
where I can get the id and the name, but I can't get the image.
Here is some code samples:
private class MyCustomAdapter extends BaseAdapter {
public ArrayList<Exercise> exercises ;
public MyCustomAdapter(ArrayList<Exercise> exercises) {
this.exercises=exercises;
}
@Override
public int getCount() {
return exercises.size();
}
@Override
public String getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater mInflater = getLayoutInflater();
View myView = mInflater.inflate(R.layout.detail_item, null);
final Exercise s = exercises.get(position);
TextView txtid=( TextView)myView.findViewById(R.id.tv_id);
txtid.setText(""+s.id);
TextView txtname=( TextView)myView.findViewById(R.id.tv_name);
txtname.setText(s.name);
ImageView img = (ImageView) myView.findViewById(R.id.img_id);
img.setImageResource(s.img);
return myView;
}
}
The error is here:
(ImageView img = (ImageView) myView.findViewById(R.id.img_id); img.setImageResource(s.img);)
The Exception:
::: Process: com.example.simo.gym_programs, PID: 11826 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at com.example.simo.gym_programs.Detail_Activity$MyCustomAdapter.getView(Detail_Activity.java:73)