maurycyw/StaggeredGridView

NullPointerException when number of child Views decreases

Closed this issue · 4 comments

hi guys,

sorry in advance to @briangriffey for still being a noob who can't do pull requests.

so basically, StaggeredGridView.layoutChildren() can trigger an NPE when it removes child Views because it iterates on the original number of child Views. the fix is super super simple and i don't have time to learn git so i'm posting it here with fingers crossed that one of the wonderful contributors will add it into the main branch.

both changes are in StaggeredGridView.layoutChildren()

first, we adjust the looping condition.
final int childCount = getChildCount();
int amountRemoved = 0;

for (int i = 0; i < childCount; i++) {
    View child = getChildAt(i);

becomes

final int childCount = getChildCount();
int amountRemoved = 0;

for (int i = 0; i < childCount-amountRemoved; i++) {
    View child = getChildAt(i);
second, we adjust the increment so we don't skip over new ith element.
// child has been removed
removeViewAt(i);
if(i-1>=0) invalidateLayoutRecordsAfterPosition(i-1);
amountRemoved++;
continue;

becomes

// child has been removed
removeViewAt(i);
if(i-1>=0) invalidateLayoutRecordsAfterPosition(i-1);
amountRemoved++;
i--;
continue;

and that's it. hope this helps :)

Please send a pull request for any code submissions.

seriously dude. i took time i don't have to type all this up just to make the world a better place. the fix is 20 keystrokes and i'm guessing you can make a pull request in 20 seconds. why kick my teeth in when you could meet me halfway and make this project even better?

@accounts01 as much as I would love to dig through the code and manually enter your diff; I have a lot going on. Considering that we are on a system that is made specifically to do code merging, I find it completely acceptable to request that you use that system to submit a patch. I appreciate the effort that you have taken the time to find a bug and fix it; however, it should not take that much longer to submit an actual pull request to fix the issue.

thanks for answering :) your request isn't unacceptable at all. this https://help.github.com/articles/using-pull-requests just seems overwhelming to me. i'll try to go through it asap though and submit a request. cheers.