Android-具有Dagger2的Kotlin,lateinit属性组件尚未初始化

时间:2018-10-29 00:44:48

标签: android kotlin dagger-2

我是Dagger新手,使用它时出现以下错误。

  

致命异常:主要                                                                      流程:com.biolabsalta.app,PID:8028                                                                      java.lang.RuntimeException:无法启动活动   ComponentInfo {com.lab.app/com.lab.app.ui.login.LoginActivity}:   kotlin.UninitializedPropertyAccessException:lateinit属性   组件尚未初始化

当我将项目从java传递到kotlin时,发生了此错误。 共享我的代码。请帮助

ActivityComponent.kt

@PerActivity
@Component(dependencies = arrayOf(ApplicationComponent::class),
        modules = arrayOf(ActivityModule::class))
interface ActivityComponent {

    @ActivityContext
    fun context(): Context

    fun inject(activity: MainActivity)

    fun inject(activity: LoginActivity)

    ...
}

ActivityModule.kt

@Module
class ActivityModule constructor(private val activity: AppCompatActivity) {

    @Provides
    @ActivityContext
    fun provideContext(): Context = activity

    @Provides
    fun provideActivity(): AppCompatActivity = activity

    @Provides
    @PerActivity
    fun provideMainPresenter(
            presenter: MainPresenter<MainMvpView>): MainMvpPresenter<MainMvpView> = presenter

    @Provides
    @PerActivity
    fun providerLoginPresenter(
            presenter: LoginPresenter<LoginMvpView>): LoginMvpPresenter<LoginMvpView> = presenter
...
}

BaseActivity.kt

abstract class BaseActivity : AppCompatActivity(), MvpView {

    lateinit var activityComponent: ActivityComponent

    override val isNetworkConnected: Boolean
        get() = NetworkUtils.isNetworkConnected(applicationContext)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityComponent = DaggerActivityComponent.builder()
                .activityModule(ActivityModule(this))
                .applicationComponent(LabApp.component)
                .build()
    }

LoginActivity.kt

class LoginActivity : BaseActivity(), LoginMvpView {

    @Inject
    lateinit var presenter: LoginMvpPresenter<LoginMvpView>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        activityComponent.inject(this)

        presenter.onAttach(this)

        btn_login.setOnClickListener{
            presenter.onServerLoginClick(account_email!!.text.toString(),
                    account_password.text.toString())
            hideKeyboard()
        }

LabApp.kt

class LabApp : Application() {

    companion object {
        lateinit var component: ApplicationComponent
    }

    override fun onCreate() {
        super.onCreate()

        component = DaggerApplicationComponent.builder()
                .applicationModule(ApplicationModule(this))
                .build()

        component.inject(this)
    }
}

有人可以帮助我吗? 预先感谢。

更新 我只是忘了在清单中声明。

android:name=".LabApp"

1 个答案:

答案 0 :(得分:0)

  

我对匕首一无所知,但是

您正在呼叫

anno::copyright_notice("XXXXX")
anno::author("Som\"e\"one")
anno::contributor("")
state::texture_coordinate(0)
state::texture_tangent_u(0)
state::texture_tangent_v(0)
[anno]::[copyright_notice]
 - argument: "XXXXX"
[anno]::[author]
 - argument: "Som\"e\"one"
[anno]::[contributor]
 - argument: ""
[state]::[texture_coordinate]
 - argument: 0
[state]::[texture_tangent_u]
 - argument: 0
[state]::[texture_tangent_v]
 - argument: 0

presenter.onAttach(this) 尚未初始化时。

LoginMvpPresenter<T>