我正在制作一个自定义GridView
适配器,它设置FrameLayout
及其 UI 元素(图像)。适配器本身并不复杂,但我得到了 compile-time error Variable imgThumb have not been initialized
。更糟糕的是,代码与Google Developer GridView 帮助页面上的代码完全相同。
这是我的适配器:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int mGroupId;
private Bitmap[] rescaledImages;
private Integer[] which;
public ImageAdapter(Context c, int groupId) {
mContext = c;
mGroupId = groupId;
//.. do init of rescaledImages array
}
public int getCount() {
return rescaledImages.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View frameLayout;
ImageView imgThumb;
if (convertView == null) { // if it's not recycled, initialize some attribute
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
frameLayout = inflater.inflate(R.layout.group_grid_item, null);
frameLayout.setLayoutParams(new AbsListView.LayoutParams(130, 130));
frameLayout.setPadding(0, 10, 0, 10);
imgThumb = (ImageView) frameLayout.findViewById(R.id.grid_item_thumb);
} else {
frameLayout = (FrameLayout) convertView;
}
imgThumb.setImageBitmap(rescaledImages[position]); //<-- ERRROR HERE!!!
return frameLayout;
}
//...
现在,我知道我可以设置方法ImageView imgThumb=null;
,getView()
但我不确定为什么这个示例适用于 Android 开发人员帮助页面。
另外我不确定我是否应该把它imgThumb
永远存在null
- 这会导致运行时错误吗?