Cole Reichenberg
Cole Reichenberg

Reputation: 23

GridView crashing in getView method when scrolled

When I run my Activity to show the below GridView it seems to get stuck on position 0.

You can see the logs below for what is happening when the GridView is displayed on screen. This works fine when it first loads but when it scrolls it seems to be trying to reuse the first position (0) even though it's still in use and crashes with a null pointer exception.

Why would it be trying to reuse the view right away? And why would it go thought the else block multiple times in a row? Is it that the Adapter gets confused by the CheckableButton which extends FrameLayout?

Thanks!

public View getView(int position, View convertView, ViewGroup parent) {
    CheckableButton checkableButton;
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(175, 175));
        checkableButton = new CheckableButton(mContext);
        checkableButton.setLayoutParams(new GridView.LayoutParams(
                GridView.LayoutParams.WRAP_CONTENT,
                GridView.LayoutParams.WRAP_CONTENT));
        checkableButton.addView(imageView);

        Log.d("Initiated New View:", "" + position);
    }
    else {
        checkableButton = (CheckableButton) convertView;
        imageView = (ImageView) checkableButton.getChildAt(position);
        Log.d("Reused View:", "" + position);
    }

    imageView.setImageBitmap(photos[position]);

    return checkableButton;
}

Here is the Log.d readouts

D/Initiated New View:﹕ 0

D/Reused View:﹕ 0

D/Reused View:﹕ 0

D/Reused View:﹕ 0

D/Initiated New View:﹕ 1

D/Initiated New View:﹕ 2

D/Initiated New View:﹕ 3

D/Initiated New View:﹕ 4

D/Initiated New View:﹕ 5

D/Initiated New View:﹕ 6

D/Initiated New View:﹕ 7

D/Initiated New View:﹕ 8

D/Initiated New View:﹕ 9

D/Initiated New View:﹕ 0

D/Reused View:﹕ 0

D/Reused View:﹕ 0

D/Reused View:﹕ 0

D/Reused View:﹕ 0

D/Reused View:﹕ 0

Upvotes: 1

Views: 80

Answers (3)

Kevin Constantine
Kevin Constantine

Reputation: 262

The position arg in getView method is actually the index of each visible items displayed on your screen. So for example, if you have 20 items in your adapter, your position arg at scroll mode gets to 20 at the end of the list. In your implementation checkableButton has only one child, so it fails when the position gets bigger than checkableButton child view count. try doing something like this instead

imageView = (ImageView) checkableButton.getChildAt(0);

since your imageView is the only childView of checkableButton which is accessed with the 0 index.

Upvotes: 5

Derek Fung
Derek Fung

Reputation: 8211

D/Reused View:﹕ 0

First, this means it is reusing some other convertView to create 0 again, not it is creating position 0 from itself.

Your crash probably caused by this

imageView = (ImageView) checkableButton.getChildAt(position);

the checkableButton always has 1 child, so it will crash if position != 0

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157487

the problem is this line

 imageView = (ImageView) checkableButton.getChildAt(position);

your CheckableButton has only one child, the ImageView you added when convertView is null. So you should retrieve the child at position 0. The children of your CheckableButton are not related with the position you get in getView. You should use that value to access the dataset.

Upvotes: 2

Related Questions