首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >延迟属性androidInjector尚未初始化Dagger 2

延迟属性androidInjector尚未初始化Dagger 2
EN

Stack Overflow用户
提问于 2020-03-10 23:25:13
回答 2查看 890关注 0票数 0

我有这样的申请

代码语言:javascript
复制
class App : Application() , HasAndroidInjector {

    lateinit var application: Application

    @Inject
    lateinit var androidInjector : DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> = androidInjector

    override fun onCreate() {
        super.onCreate()
        this.application = this
    }
}

那我就有了AppComponent

代码语言:javascript
复制
@Singleton
@Component(
    modules = [
        AndroidSupportInjectionModule::class,
        LoginModule::class
    ]
)
interface AppComponent : AndroidInjector<App> {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: App): AppComponent
    }
}

LoginModule是这样的

代码语言:javascript
复制
@Module
abstract class LoginModule {

    @ContributesAndroidInjector(modules = [LoginDependencies::class])
    abstract fun bindLoginFragment(): LoginFragment
}

在我使用的onViewCreated of Fragment

代码语言:javascript
复制
AndroidSupportInjection.inject(this)

我也尝试过将它添加到onAttach()上,但是它不起作用。一旦我添加了

代码语言:javascript
复制
@Inject
lateinit var loginPresenter: LoginContract.Presenter

如果我删除它,它和问题一样,如果我离开演示者,它说没有初始化预置器。

是不是我错过了什么?

编辑

我在哪里得到了这个错误

引发的

:kotlin.UninitializedPropertyAccessException: lateinit属性androidInjector尚未初始化

override fun androidInjector(): AndroidInjector<Any> = androidInjector

那我也会得到这个

com.testing.login.login.presentation.LoginFragment.onRequestInjection(LoginFragment.kt:31)的

AndroidSupportInjection.inject(this)

这个onRequestInjection位于onViewCreated中的一个AbstractFragment中,我也尝试过将它放在onAttach()上,但是没有什么改变。

我的build.gradle包含:

这个在:app中

代码语言:javascript
复制
    api(LibrariesDependencies.DAGGER)
    api(LibrariesDependencies.DAGGER_ANDROID)
    api(LibrariesDependencies.DAGGER_ANDROID_SUPPORT)
    kapt LibrariesDependencies.DAGGER_ANDROID_KAPT
    kapt LibrariesDependencies.DAGGER_KAPT
    kapt LibrariesDependencies.DAGGER_ANNOTATION_PROCESSOR

登录:登录

代码语言:javascript
复制
api(LibrariesDependencies.DAGGER)
    api(LibrariesDependencies.DAGGER_ANDROID)
    api(LibrariesDependencies.DAGGER_ANDROID_SUPPORT)
    kapt LibrariesDependencies.DAGGER_ANDROID_KAPT
    kapt LibrariesDependencies.DAGGER_KAPT
    kapt LibrariesDependencies.DAGGER_ANNOTATION_PROCESSOR
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-11 09:06:23

未初始化的属性是否发生在App类中?如果是这样的话,这可能意味着在onCreate类的App方法中缺少以下内容。

代码语言:javascript
复制
DaggerAppComponent
            .factory()
            .create(this)
            .inject(this)
票数 1
EN

Stack Overflow用户

发布于 2020-03-11 09:09:06

在您的应用程序类中,您定义了一个@Inject属性,但没有在任何地方初始化它。您需要做的是:首先将App声明为Dagger目的的入口点:

代码语言:javascript
复制
interface AppComponent : AndroidInjector<App> {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: App): AppComponent
    }

    fun inject(app: App) // Let Dagger know your Application class with root dispatching injector
}

然后实际创建AppComponent实例并使用它引导Dagger并注入DispatchingAndroidInjector实例:

代码语言:javascript
复制
override fun onCreate() {
  super.onCreate()
  this.application = this

  DaggerAppComponent.factory().create(this).inject(this)
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60627262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档