viewModelFactory lateinit has not been initialized errpr
when injecting a dagger view model factory into a fragment and initialising the viewmodel using viewmodel lazy delegate property, here is a snippet of my code..
Error :
Copy code
Fatal Exception: kotlin.UninitializedPropertyAccessException
lateinit property viewModelFactory has not been initialized
Code:
Copy code
class DeliveriesFragment() : Fragment(){
@Inject
lateinit var daggerViewModelFactory: DaggerViewModelFactory
private val upcomingDeliveriesViewModel : UpcomingDeliveriesViewModel by viewModels { daggerViewModelFactory }
override fun onAttach(context: Context) {
SalesApplication.salesComponent().inject(this)
super.onAttach(context)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
observeDeliveries()
}
private fun observeDeliveries(){
upcomingDeliveriesViewModel.loadUpcomingUserDeliveries("").observe(viewLifecycleOwner, Observer {
//displayList()
})
}
}
s
stephanmg
07/02/2020, 2:54 PM
What is lateinit exactly doing?
k
kursivee
07/02/2020, 9:36 PM
the error references
viewModelFactory
but that class has
daggerViewModelFactory
is it somewhere else? the code looks correct though.
➕ 1
h
Henry
07/03/2020, 4:23 AM
Are you declare
DeliveriesFragment
in DI Module?
j
Joost Klitsie
07/03/2020, 11:21 AM
are you compiling with java 1.8?
m
marios proto
07/03/2020, 1:15 PM
check that you have the DaggerVMFactory in your graph.
call this on onCreate
Copy code
SalesApplication.salesComponent().inject(this)
marios proto
07/03/2020, 1:16 PM
even better consider to support FragmentFactory and move to constructor injection rather than properties injection