NullPointerException initStickyHeadersHolder on orientation change
See original GitHub issueSituation:
- 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:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@skimarxall, ah the breakpoint is in
onCreate
not aftersetStickyHeaders
. I will retry.Thanks for fixing it.