0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sandalone
  • 41,141
  • 63
  • 222
  • 338

3 Answers3

1

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]);.

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
  • Thanks. You lead me to the solution (upvoted). I posted the solution below as a reference to future people in same situation as myself – sandalone Jul 06 '13 at 09:34
  • 1
    You are welcome, but technically that is the answer to your question, `Why am I getting “ImageView not initialized” error in GridiView custom adapter?` because of the whole in the code logic as explained. – LuckyMe Jul 06 '13 at 09:38
  • Yes, but you should have warned me that I am not initializing gridview item properly :). That's why I said "you lead me to" the real problem. Funny thing is that Google always gives the simplest sample and I also did not find the way to deal with the multiple UI elements in the gridview item. – sandalone Jul 06 '13 at 15:14
0

This will happen only when your convertView is not null! So,you'll have to initialize imgThumb outside 'if' clause.

Kunal S. Kushwah
  • 883
  • 1
  • 8
  • 19
  • 1
    No, there is a better and proper solution. Look at my reply. In your case images will start appearing randomly while you scrollw gridview. TRy it. – sandalone Jul 06 '13 at 09:35
0

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!!!
    }
sandalone
  • 41,141
  • 63
  • 222
  • 338