Home » Java » NullPointerException in dissapearence animation of RecyclerView from support v.23.2.0

NullPointerException in dissapearence animation of RecyclerView from support v.23.2.0

Posted by: admin December 28, 2021 Leave a comment

Questions:

In new RecyclerView we can use wrap_content for height (or for width). So Google fixed bug – https://code.google.com/p/android/issues/detail?id=74772

But not all is well in the end.

I try to use animation of dissapearence when remove item from RecyclerView.
So i use method of Adapter – notifyItemRemoved(int) with any argument and RecyclerView cause NullPointerException.

Xml code:

...
    <RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:overScrollMode="always"/>
    ...

Logs:

E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.NullPointerException
     at android.support.v7.widget.SimpleItemAnimator.animateDisappearance(SimpleItemAnimator.java:78)
     at android.support.v7.widget.RecyclerView.animateDisappearance(RecyclerView.java:3246)
     at android.support.v7.widget.RecyclerView.access$700(RecyclerView.java:147)
     at android.support.v7.widget.RecyclerView$4.processDisappeared(RecyclerView.java:422)
     at android.support.v7.widget.ViewInfoStore.process(ViewInfoStore.java:231)
     at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3086)
     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2914)
     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
     at android.view.View.layout(View.java:14015)
     at android.view.ViewGroup.layout(ViewGroup.java:4373)
     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
     at android.view.Choreographer.doFrame(Choreographer.java:532)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
     at android.os.Handler.handleCallback(Handler.java:725)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:5227)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:511)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
     at dalvik.system.NativeStart.main(Native Method)

Maybe there are temp workarounds of these problem?

EDIT

I tried to reproduce in new project. So no exceptions was thrown. Maybe in my project there are others dependencies which call this problem.

Answers:

I had exactly the same error as the one raised in the question and in fact have raised a bug with Google for this. I did find, after delving into the framework code, that I could resolve the issue by changing the setting of HasFixedSize from FALSE to TRUE i.e. “rv.setHasFixedSize(true)”. This is basically telling the RecyclerView that changes to its contents will not change the RecyclerView size thus avoiding a full layout. I had originally set this FALSE due to a mis-interpretation of its meaning. This change removes the fault for me and is a reproducible solution.

###

Google has made some statement about the usage of the RecyclerView with the version 23.2 of the Support Library.

Link to the blog post (check the RecyclerView section): http://android-developers.blogspot.com.br/2016/02/android-support-library-232.html

Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

I would suggest you to try to change your RecyclerView’s layout_height param to match_parent. Let us know if it works.

###

I have exactly the same exception. It occurs when I add a view to the RecyclerView and that view is completely beyond the boundaries of the RecyclerView (user would have to scroll to see that newly added view).

However, I’ve noticed that this exception only occurs when width or height of the RecyclerView is wrap_content or 0dp with weight set to 1 inside a LinearLayout… If I set the width and height to any other value except those two, i.e. 140dp, match_parent, then everything works fine.

So, I’ve tried to trick the buggy framework and find a solution to set the width of my RecyclerView to be as big as there are space left inside my LinearLayout that contains the RecyclerView (width:0dp, weight:1 combination), so I wrapped the RecyclerView inside another LinearLayout with weight set to 1, and set my RecyclerView‘s width to match_parent, so it gets as wide as parent LinearLayout can get, and the parent LinearLayout fills the free space in another parent LinearLayout. And voila, it works perfectly!

I know, it sounds ridiculous, but that’s the way I worked it out…

Here’s the source code if anybody needs to get the idea of how to workaround this bug…

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/pic_grid"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:background="#BBB"/>

    </LinearLayout>

    <Button
        android:id="@+id/pic_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pic_button"/>

</LinearLayout>

The RecyclerView now is as wide as there are free space in the parent LinearLayout, which also contains one button to the right of the RecyclerView, and no crashes occurs.

If anybody finds out the real cause of such an exception or a better way to fix it, please share…