简体   繁体   中英

Dagger2 not inject

I am a newbie to android and trying to use Dagger2. I spend whole night and still dont know why my dagger does not provide presenter. Here are my code (I use Kotlin)

AppComponent

@Singleton
@Component(modules = arrayOf(PresenterModule::class))
interface AppComponent {

    fun inject(target: SplashActivity)

}

PresenterModule

@Module
class PresenterModule {

    @Provides
    @Singleton
    fun provideSplashPresenter(): SplashPresenter {
        return SplashPresenter()
    }
}

App

class App: Application() {

    companion object {
        lateinit var appComponent: AppComponent
    }

    override fun onCreate() {
        super.onCreate()
        appComponent = initDagger()
    }

    private fun initDagger(): AppComponent {
        return DaggerAppComponent.create()
    }
}

This is the presenter

class SplashPresenter: BasePresenterImpl<SplashContract.View>(), SplashContract.Presenter {

    override fun performToast(mess: String) {
        logi("abc", "performToast")
        logi("abc", "mess: " + mess)
        mView?.showLoading()
        if (mess.isNullOrBlank()) {
            mView?.showTosat("this is empty mess") ?: logi("abc", "null")
        } else {
            mView?.showTosat(mess) ?: logi("abc", "null")
        }
        mView?.hideLoading()

    }
}

And finally, this is my SplashActivity

class SplashActivity : BaseActivity(), SplashContract.View {


    @Inject
    lateinit var presenter: SplashPresenter

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

        presenter.attachView(this)

        //TODO: check log in
        //TODO: If logged in => start main screen
        //TODO: If not logged in => load login activity

        button.setOnClickListener{
            presenter.performToast(editText.text.toString())
            logi("abc", "perform clicked")
        }
    }
}

When I run these code, I got this error Lateinit property presenter has not been initialized, which means that "Inject" does not work

Since you're not using constructor injection here (which you can't, because you don't 'own' the activity's constructor) Dagger does not 'know' that it has to inject something into your Activity . You have to manually inject like this:

(applicationContext as App).appComponent.inject(this) 

in your SplashActivity 's onCreate() method (before using the presenter, of course).

Second, your presenter needs a constructor that tells Dagger how to construct/'build' the presenter, which means a constructor annotated with the @Inject annotation, so:

class SplashPresenter @Inject constructor(): BasePresenterImpl<SplashContract.View>(), SplashContract.Presenter 

You forgot to inject the SplashActivity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    App.appComponent.inject(this)
    setContentView(R.layout.activity_splash)
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM