Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE onSizeChanged #41

Closed
tumb1er opened this issue Jan 29, 2014 · 4 comments
Closed

NPE onSizeChanged #41

tumb1er opened this issue Jan 29, 2014 · 4 comments

Comments

@tumb1er
Copy link

tumb1er commented Jan 29, 2014

Got this exception from Google Play Developers Console:

java.lang.NullPointerException
    at com.etsy.android.grid.StaggeredGridView.onColumnSync(StaggeredGridView.java:890)
    at com.etsy.android.grid.StaggeredGridView.onSizeChanged(StaggeredGridView.java:846)

Trying to reproduce locally, but not yet succeeded... Can I help with anything else?

@resamsel
Copy link

I just encountered a similar problem:

java.lang.NullPointerException
at com.etsy.android.grid.StaggeredGridView.onColumnSync(StaggeredGridView.java:926)
at com.etsy.android.grid.StaggeredGridView.onSizeChanged(StaggeredGridView.java:882)
at com.etsy.android.grid.ExtendableListView.onSizeChanged(ExtendableListView.java:232)
at com.etsy.android.grid.StaggeredGridView.onSizeChanged(StaggeredGridView.java:856)
at android.view.View.setFrame(View.java:14701)
at android.widget.AbsListView.setFrame(AbsListView.java:2490)

My guess is it's related to a screen orientation change, but I cannot reproduce it on my devices. The crash was reported in the Play Store:

Android version: Android 4.2
Device: Galaxy S4 Active (jactivelte)

@ajpolt
Copy link
Contributor

ajpolt commented Apr 18, 2014

I'm having the same issue. The code that is causing this is in here, in StaggeredGridView.java:

private void onColumnSync() {
// re-calc tops for new column count!
int syncPosition = Math.min(mSyncPosition, getCount() - 1);

    SparseArray<Double> positionHeightRatios = new SparseArray<Double>(syncPosition);
    for (int pos = 0; pos < syncPosition; pos++) {
        // check for weirdness
        final GridItemRecord rec = mPositionData.get(pos);
        if (rec == null) break;                                                             //<<<<<<<<< BREAK HERE //

        Log.d(TAG, "onColumnSync:" + pos + " ratio:" + rec.heightRatio);
        positionHeightRatios.append(pos, rec.heightRatio); 
    }

    mPositionData.clear();

    // re-calc our relative position while at the same time
    // rebuilding our GridItemRecord collection

    if (DBG) Log.d(TAG, "onColumnSync column width:" + mColumnWidth);

    for (int pos = 0; pos < syncPosition; pos++) {
        final GridItemRecord rec = getOrCreateRecord(pos);
        final double heightRatio = positionHeightRatios.get(pos);    // <<<<<<<<< CRASHES HERE //
        final int height = (int) (mColumnWidth * heightRatio);
        rec.heightRatio = heightRatio;

    //etc...

}

Specifically, this line is what is causing this crash for me:

final double heightRatio = positionHeightRatios.get(pos);

Above that, in the 1st for() loop, there is a "weirdness" check which breaks and moves on to the next record if the current record is null. If that condition is met, the height ratio will never be appended into positionHeightRatios at position "pos". Unfortunately, the next loop will still always check positionHeightRatios.get(pos), which, if the weirdness check succeeds, will be null for that pos. Which wouldn't be a problem, except that it's being saved into a primitive double.

Can anyone else think of a reason that this can't or shouldn't be fixed by simply adding another break in the 2nd for loop if(positionHeightRatios.get(pos) == null)?

Unfortunately I'm still unable to find a situation where the record is null, so I'm not completely sure what the side effects might be. I can't imagine they'd be worse than my app crashing, though.

ajpolt added a commit to ajpolt/AndroidStaggeredGrid that referenced this issue Apr 21, 2014
@ajpolt
Copy link
Contributor

ajpolt commented May 16, 2014

@resamsel @tumb1er it hasn't been accepted yet after almost a month, but the ridiculously simple fix I submitted has stopped all crashes due to this bug in my app. See code here: ajpolt@7699ba8

denizmveli added a commit that referenced this issue May 24, 2014
Merged pull request #91 - Fix for "NPE onSizeChanged" #41
@denizmveli
Copy link
Contributor

Fixed by #91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants