question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullPointerException initStickyHeadersHolder on orientation change

See original GitHub issue

Situation:

  • Launch Activity starts Main activity
  • Fragment added in activity on created
  • Adapter initialized onViewCreated
  • Set stickyHeaders(true)
  • Rotate the device while Main activity has not fully created (timing is difficult, use debugger to stop onCreate and then rotate device)
  • Fragment gets recreated together with the adapter
  • Old adapter execute posted runnable and tries to add sticky header in a null view
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference
                                                                        at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.initStickyHeadersHolder(StickyHeaderHelper.java:104)
                                                                        at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.attachToRecyclerView(StickyHeaderHelper.java:77)
                                                                        at eu.davidea.flexibleadapter.FlexibleAdapter$5.run(FlexibleAdapter.java:1357)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:158)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

The problem is this piece of code

    public FlexibleAdapter<T> setStickyHeaders(final boolean sticky, @Nullable ViewGroup stickyContainer) {
        log.i("Set stickyHeaders=%s (in Post!)%s", sticky, (stickyContainer != null ? " with user defined Sticky Container" : ""));

        // With user defined container
        mStickyContainer = stickyContainer;

        // Run in post to be sure about the RecyclerView initialization
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // Enable or Disable the sticky headers layout
                if (sticky) {
                    if (mStickyHeaderHelper == null) {
                        mStickyHeaderHelper = new StickyHeaderHelper(FlexibleAdapter.this,
                                mStickyHeaderChangeListener, mStickyContainer);
                        mStickyHeaderHelper.attachToRecyclerView(mRecyclerView);
                        log.i("Sticky headers enabled");
                    }
                } else if (areHeadersSticky()) {
                    mStickyHeaderHelper.detachFromRecyclerView();
                    mStickyHeaderHelper = null;
                    log.i("Sticky headers disabled");
                }
            }
        });
        return this;
    }

Since you are posting a runnable if the timing is good it will be executed after onDestroyView.

Possible solutions, check if recylcer is still there or remove the runnable.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
davideascommented, Dec 21, 2017

@skimarxall, ah the breakpoint is in onCreate not after setStickyHeaders. I will retry.

0reactions
marcelpintocommented, Jan 8, 2018

Thanks for fixing it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullPointerException when the orientation changes
I have a Preference activity where I get this error when it changes orientation: java.lang.NullPointerException: Attempt to invoke virtual ...
Read more >
ViewPager Null Pointer Exception when orientation changes ...
I first detected this issue when using JakeWharton ViewPager Library but believe the issue lies in the actually compatibility library.
Read more >
Device Orientation - Xamarin - Microsoft Learn
SensorLandscape – causes the application to use landscape orientation while using sensor data to change the direction the screen is facing (so ...
Read more >
NullPointerException: Attempt to invoke virtual me...
NullPointerException : Attempt to invoke virtual method 'void. ... found that their app was incorrectly handling the orientation changes.
Read more >
Managing screen orientation - Web APIs - MDN Web Docs
Adjusting layout based on the orientation. One of the most common cases for orientation changes is when you want to revise the layout...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found