UI 要素 (画像)を設定するカスタムGridView
アダプターを作成しています。FrameLayout
アダプター自体は複雑ではありませんが、コンパイル時エラーが発生します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
これは実行時エラーを引き起こす可能性がありますか?