我正在使用来自应用程序中最新android架构组件的Jetpack导航。在极少数情况下,我会发生以下崩溃:
Fatal Exception: java.lang.IllegalStateException: Fragment NewOfferFragment{40b548a} not attached to a context.
at android.support.v4.app.Fragment.requireContext(Fragment.java:609)
at android.support.v4.app.Fragment.getResources(Fragment.java:7673)
at com.abc.xyz.offer.owner.fragment.NewOfferFragment$onActivityCreated$2.onChanged(NewOfferFragment.kt:1089)
at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at android.arch.lifecycle.Transformations$2$1.onChanged(Transformations.java:145)
at android.arch.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at com.abc.xyz.util.remoteUtils.NetworkBoundResource$fetchFromNetwork$2$2.com.abc.xyz.util.remoteUtils.Resource$Companion.error(NetworkBoundResource.kt:3019)
at android.arch.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:121)
at android.arch.lifecycle.LiveData.access$000(LiveData.java:59)
at android.arch.lifecycle.LiveData$ObserverWrapper.shouldBeActive(LiveData.java:416)
at android.arch.lifecycle.LiveData.observeForever(LiveData.java:207)
at android.arch.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
at android.arch.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:96)
at com.abc.xyz.util.remoteUtils.NetworkBoundResource$fetchFromNetwork$2.onChanged(NetworkBoundResource.kt:1074)
at android.arch.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
at android.arch.lifecycle.LiveData$1.run(LiveData.java:87)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
以下是我片段的onActivityCreated的代码:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this, viewModelFactory)[NewOfferViewModel::class.java]
viewModel.init(offerId)
binding?.let {
it.viewModel = viewModel
it.handler = RadioButtonHandler(this)
}
viewModel.result.observe(this, Observer {
it?.let {
binding.resource = it
endProgress()
Logger.o(Thread.currentThread(), it)
when (it.status) {
Status.SUCCESS -> {
Logger.i(Thread.currentThread(), it.message)
endProgress()
if (viewModel.shouldUpload)
navController().popBackStack()
viewModel.shouldUpload = false
}
Status.ERROR -> {
Logger.i(Thread.currentThread(), it.message)
endProgress()
navController().popBackStack()
viewModel.shouldUpload = false
}
Status.LOADING -> {
startProgress()
}
}
}
})
}
现在,我已经看到了许多解决此崩溃的可能原因的方法,但是这些方法是用于通过片段管理器填充片段的传统方法的。使用jetpack导航时,导致此崩溃的可能原因是什么?
谢谢。