我为ListView
制作了一个适配器,我可以在其中获取ID和名称,但我无法获取图像。
以下是一些代码示例:
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;
}
}
错误在于:
(ImageView img =(ImageView) myView.findViewById(R.id.img_id); img.setImageResource(s.img))
例外:
::: Process:com.example.simo.gym_programs,PID:11826 java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.ImageView.setImageResource(int)&#39;在null对象引用上 at com.example.simo.gym_programs.Detail_Activity $ MyCustomAdapter.getView(Detail_Activity.java:73)