Reputation: 7081
I cannot seem to find the origin of this DataBinding
NullPointerException
. While using Android Navigation Architecture
when navigating to a Fragment
and navigating up again repetitively I will eventually end up with the following stack trace
java.lang.NullPointerException: Attempt to invoke direct method 'void androidx.databinding.ViewDataBinding.handleFieldChange(int, java.lang.Object, int)' on a null object reference
at androidx.databinding.ViewDataBinding.access$800(ViewDataBinding.java:64)
at androidx.databinding.ViewDataBinding$LiveDataListener.onChanged(ViewDataBinding.java:1592)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:424)
at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:376)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:355)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:293)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:333)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:138)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:124)
at androidx.fragment.app.Fragment.performStart(Fragment.java:2485)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1494)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
It looks like it has something to do with the LiveData
lifecycle as stated in the exception. I'm guessing onPause
the LiveData
objects in the ViewModel
are cleared for memory sake and are then for some reason accessed again.
This seems to be very random as it happens on all of our fragments but only when continuously navigating to and from that specific fragment or between different fragments in the app. We don't attempt to access LiveData
objects after the fragment is paused/destroyed. I have not been able to find anyone who has experienced this problem before, and find it quite difficult to get to the root of the issue.
Upvotes: 19
Views: 9045
Reputation: 446
I encountered this same issue this morning and was glad to see I wasn't the only one.
I think I've resolved it for myself although it's a little hard to be sure as you'll know. For me it was when paging between fragments in a viewPager that I would sometimes get the error. I believe the fragments were becoming detached when they were offscreen but databinding updates were still being called. I confirmed this by setting the viewPager's offscreenPageLimit to 0 and I started getting the error far more consistently.
My solution was to replace:
binding.setLifecycleOwner(this);
With:
binding.setLifecycleOwner(getViewLifecycleOwner());
Get a LifecycleOwner that represents the Fragment's View lifecycle. In most cases, this mirrors the lifecycle of the Fragment itself, but in cases of detached Fragments, the lifecycle of the Fragment can be considerably longer than the lifecycle of the View itself.
I haven't encountered the error again since making this change myself, so please let me know if this fixes the issue for you. I will update my answer if I do finally get the error again, but so far I am confident that this has fixed it in my case.
Upvotes: 33