这是我的基本适配器,我正在尝试使用drawable中的图像来扩充GridView。事实证明,它返回NullPointerExceptipn。但是当我将getCount()返回值更改为0时,它不再显示异常,但gridview没有显示。我已经取出了build_list的日志,里面有数据。
public class BuildingListAdapter extends BaseAdapter {
Context context;
ArrayList<Building> build_list = new ArrayList<Building>();
public BuildingListAdapter(Context c, ArrayList<Building> build_list) {
// TODO Auto-generated constructor stub
context = c;
this.build_list.addAll(build_list);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return build_list.size();
}
@Override
public Building getItem(int position) {
// TODO Auto-generated method stub
return build_list.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.v("CheckifRun", "true");
Building building = this.getItem(position);
ImageView img_view = new ImageView(convertView.getContext());
int resID = convertView.getResources().getIdentifier(building.getBuild_img(), "drawable", "com.vapp.yangonuniversity");
Log.v("resourceID", Integer.toString(resID));
img_view.setImageResource(resID);
img_view.setScaleType(ScaleType.CENTER_CROP);
img_view.setLayoutParams(new GridView.LayoutParams(70, 70));
return img_view;
}
}
答案 0 :(得分:1)
将getView方法更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.v("CheckifRun", "true");
Building building = this.getItem(position);
ImageView img_view = new ImageView(context);
int resID = context.getResources().getIdentifier(building.getBuild_img(), "drawable", "com.vapp.yangonuniversity");
Log.v("resourceID", Integer.toString(resID));
img_view.setImageResource(resID);
img_view.setScaleType(ScaleType.CENTER_CROP);
img_view.setLayoutParams(new GridView.LayoutParams(70, 70));
return img_view;
}
您需要创建ImageView
的上下文并从资源
改变了什么?
ImageView img_view = new ImageView(context);
和
int resID = context.getResources().getIdentifier(building.getBuild_img(), "drawable", "com.vapp.yangonuniversity");
<强>更新强>
你得到OutOfMemomryException
,
<强>为什么强>
当您的应用超出堆中分配的内存时,会发生OutofMemory。位图太大而无法放入内存即堆
如何解决此问题?
你可以在谷歌搜索一下找到许多相关的问题,首先你可以看到:
Strange out of memory issue while loading an image to a Bitmap object