Reputation: 41759
I am making a custom GridView
adapter which sets FrameLayout
and its UI alements (images). The adapter itself is nothing complex, but yet I get compile-time error Variable imgThumb have not been initialized
. What is worse, the code is exactly the same as on Google Developer GridView help page.
Here is my adapter:
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;
}
//...
Now, I know that I can set ImageView imgThumb=null;
in getView()
method, but I am not sure why this example works on Android developer help page.
Also I am not sure if I should put that imgThumb
is ever null
- can this make a runtime error?
Upvotes: 0
Views: 2156
Reputation: 41759
Got it thanks to @LuckyMe (thou he did not reply the whole solution).
Anyway, for those who like me want to initialize and use root element of the GridView
cell and its children, you should take care to default initialize each child element as well in the else
by using convertView.findViewById()
method.
Namely, my code must be fixed like this:
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 = (ImageView) convertView.findViewById(R.id.grid_item_thumb); //INITIALIZE EACH CHILD AS WELL LIKE THIS!!!
}
Upvotes: 0
Reputation: 3910
That code is NOT like the one you linked to, that code should crash even if you set imgThumb = null
. Because in the case where convertView != null
, imgThumb
will never be set to anything, and therefore crash on the line imgThumb.setImageBitmap(rescaledImages[position]);
.
Upvotes: 1
Reputation: 893
This will happen only when your convertView is not null! So,you'll have to initialize imgThumb outside 'if' clause.
Upvotes: 0