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.