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();
}
Miguel Botón
  • 766
  • 5
  • 20

2 Answers2

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.

Miguel Botón
  • 766
  • 5
  • 20
  • 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. – Kartik Apr 21 '15 at 03:03
  • 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. – Miguel Botón Apr 21 '15 at 16:51
  • 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. – andreasv Apr 22 '15 at 08:14
  • Hey, I have the same problem and I created a issue here https://issuetracker.google.com/issues/188096921 It would be nice if you folks could star the issue, so the android team gives it more visibility. – BugsBunnyBR May 14 '21 at 10:20
0

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

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

KaMyLL
  • 969
  • 1
  • 7
  • 13