NullPointerException in StaggeredGridLayoutManager [duplicate]
Asked Answered
E

2

6

I'm getting some crash reports about a NullPointerException happening in StaggeredGridLayoutManager. Looks like around 10% of my users are suffering this issue.

I have a very basic RecyclerView with a basic adapter, nothing special, and I did try to reproduce this error in my devices with no luck at all.

This is the raw report I'm getting reported:

java.lang.NullPointerException
   at android.support.v7.widget.StaggeredGridLayoutManager.recycleFromStart(StaggeredGridLayoutManager.java:1661)
   at android.support.v7.widget.StaggeredGridLayoutManager.recycle(StaggeredGridLayoutManager.java:1529)
   at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1471)
   at android.support.v7.widget.StaggeredGridLayoutManager.scrollBy(StaggeredGridLayoutManager.java:1846)
   at android.support.v7.widget.StaggeredGridLayoutManager.scrollVerticallyBy(StaggeredGridLayoutManager.java:1764)
   at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3062)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
   at android.view.Choreographer.doCallbacks(Choreographer.java:603)
   at android.view.Choreographer.doFrame(Choreographer.java:572)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:157)
   at android.app.ActivityThread.main(ActivityThread.java:5335)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
   at dalvik.system.NativeStart.main(NativeStart.java)

I have no clue about what is happening. I thought that maybe I was setting the adapter before the layout manager but nope, it's not the case.

I hope somebody can throw some light over this.

Thanks in advance!

UPDATE:

I'm copying some code from my app related to the RecyclerView.

This code is from a Fragment:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    RecyclerView.LayoutManager layout = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)

    mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(layout);
    mRecyclerView.setAdapter(mAdapter);
}

And this is from the adapter, specifically the method that sets the adapter items:

public void setItems(List<String> items) {
    mItems.clear();
    mItems.addAll(items);

    notifyDataSetChanged();
}
Eclipse answered 26/3, 2015 at 20:53 Comment(5)
Could you please add some code (acc. excpetion around StaggeredGridLayoutManager.java, line1661)?Luncheon
All that related code is in the support library.Dittman
But how does your code look like? This exception can't appear from nowhere .... ;)Luncheon
I edited the post to add some relevant code.Dittman
Any news on this one? I am still facing this bug mainly on Android 9Felt
E
0

Well, looks like setting up the layout manager and the adapter in onCreateView() fixed the issue in most devices. No idea what could be happening.

I'm still getting very similar crash reports but now only from weird brands.

I hope this info can be helpful for other devs in the future.

UPDATE

Still getting an important quantity of the same crash report. This cannot be considered as fixed at all :/

I will keep looking for a fix and maybe create a sample project that suffers from this issue.

Eclipse answered 27/3, 2015 at 18:7 Comment(4)
I have the same problem. Setting up the layout manager and the adapter in onCreateView() has nothing to do with our problem, take my word for it. Were you able to solve it? I think its a bug in the Android library itself.Blastema
In my case it was an issue with Pollfish SDK. I don't know exactly what was happening but Pollfish caused this issue because of how it manipulates the UI layout. I was able to fix the issue by initializing the SDK in a different way.Dittman
Miguel I know you said you found a workaround, just wanted to mention that you can use an alternative initialization method for Pollfish where you insert the top view of your app and avoid UI manipulation.Algebraic
Hey, I have the same problem and I created a issue here issuetracker.google.com/issues/188096921 It would be nice if you folks could star the issue, so the android team gives it more visibility.Checker
P
0

I found some workaround for this issue, check my comment on Issue Tracker

https://issuetracker.google.com/issues/188096921#comment4

Pythagoras answered 27/9, 2021 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.