Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lateinit property viewModelFactory has not been initialized in Fragment

I am using dagger 2 (version 2.15) for dependency injection in my android project. Everything works fine except injecting viewModelFactory. I cannot inject it in my fragment.

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

private val mainViewModel: StationsViewModel by lazy {
    ViewModelProviders.of(this, viewModelFactory)
            .get(StationsViewModel::class.java)
}

I get the following error:

lateinit property viewModelFactory has not been initialized

I have ViewModelModule:

@Module
abstract class ViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    internal abstract fun bindsMainViewModel(mainViewModel: 
    MainViewModel): ViewModel

    @Binds
    abstract fun bindViewModelFactory(factory: MyViewModelFactory): 
    ViewModelProvider.Factory
}

ViewModelKey

@MustBeDocumented
@Target(
        AnnotationTarget.FUNCTION,
        AnnotationTarget.PROPERTY_GETTER,
        AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)

and ViewModelFactory

@Singleton
class MyViewModelFactory @Inject constructor(
        private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        val creator = creators[modelClass] ?: creators.entries.firstOrNull {
            modelClass.isAssignableFrom(it.key)
        }?.value ?: throw IllegalArgumentException("unknown model class $modelClass")
        try {
            @Suppress("UNCHECKED_CAST")
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }

    }
}

although I do not quite understand what they are doing.

like image 364
Ana Koridze Avatar asked May 02 '18 14:05

Ana Koridze


1 Answers

Can you try to initiate in onViewCreated() ?

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    mainViewModel = ViewModelProviders.of(this, viewModelFactory)
        .get(StationsViewModel::class.java)
    // binding.viewmodel = viewModel
}

Should be okay.

like image 84
Aveek Avatar answered Nov 17 '22 12:11

Aveek