在FragmentModule

问题描述

这里的匕首设置有什么问题? 我正在尝试创建MapFragment并调用test方法(然后在viewModel中调用foo方法 但这是我遇到lateinit property viewModel has not been initialized异常的问题 我有活动模块

@Module
abstract class ActivityModule private constructor() {
    @DaggerScope(FeatureFragment::class)
    @ContributesAndroidInjector(modules = [FeatureFragmentModule::class])
    abstract fun bindFeatureFragmentModule(): FeatureFragment
}

这是我的FeatureFragmentModule

abstract class FeatureFragmentModule private constructor() {

    @DaggerScope(MapFragment::class)
    @ContributesAndroidInjector(modules = [MapFragmentModule::class])
    abstract fun bindMapFragmentt(): MapFragment

     @Module
     companion object {

        @JvmStatic
        @Provides
        fun provideTestDependency(): TestDependency = TestDependency()
    }
}

这是我的MapFragmentModule

@Module
abstract class MapFragmentModule private constructor() {
    @Module
    companion object {
        @JvmStatic
        @Provides
        @IntoMap
        @ViewModelKey(MapViewModel::class)
        fun provideMapViewModelFactory(): ViewModel = MapViewModel()

        @JvmStatic
        @Provides
        fun provideViewModel(
            target: MapFragment,factory: ViewModelFactory
        ): MapViewModel =
            ViewModelProviders.of(target,factory).get(MapViewModel::class.java)

    }
}

这是我的MapFragment

class  MapFragment() : DaggerFragment() {
    @Inject
    lateinit var viewModel: MapViewModel

    fun test() {
        viewModel.foo() // crash :/
    }
}

这就是我要做的

class FeatureFragment() : DaggerFragment() {
    private lateinit var mapFragment: MapFragment

    @Inject lateinit var testDependency: TestDependency // everything is ok

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        fragment = MapFragment()
        mapFragment.test()
    }
}

有人知道我在这里想念什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)