Kotlin Android MVP + Dagger 2 lateinit属性演示者尚未初始化

时间:2018-10-09 11:52:27

标签: android kotlin dagger-2 android-mvp

尝试在Android上将Dagger与Kotlin结合使用。并得到了例外:

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized
    at com.ad.eartquakekotlin.main.MainFragment.onViewCreated(MainFragment.kt:43)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at ...

该应用程序专用于测试。我收到地震并在设备屏幕上显示。

我想要的就是 1.将演示者插入我的片段(查看) 2.将api插入我的演示者中

有我项目的结构:

enter image description here

您可以看到其中有两个模块和组件:

@Module
class ApplicationModule(private val application: Application) {
    @Provides
    @Singleton
    fun provideApplication():Application = application
}

@Module
class MainModule (private val view: MainContract.View) {
    @Provides
    fun provideView(): MainContract.View {
        return view
    }

    @Provides
    fun providePresenter(): MainContract.Presenter {
        return MainPresenter(view)
    }
}

和组件:

@Component(modules = [ApplicationModule::class])
interface ApplicationComponent {
    fun inject(application: Application)
    fun plus (mainModule: MainModule) : MainComponent
}

@Subcomponent(modules = [MainModule::class])
interface MainComponent {
    fun inject (view : MainContract.View)
}

有合同:

interface MainContract {
interface View {
    fun showLoading()
    fun hideLoading()
    fun showMessage(message: String)
    fun showData(data: EarthquakeRootObject)

}
interface Presenter {
    fun onDestroy()
    fun loadData()
}
}

应用程序类:

class MainApp: Application() {

companion object {
    lateinit var graph: ApplicationComponent
}

override fun onCreate() {
    super.onCreate()
    buildGraph()
}

private fun buildGraph() {
    graph = DaggerApplicationComponent
            .builder()
            .applicationModule(ApplicationModule(this))
            .build()
}
}

片段(我要在其中使用注入)

class MainFragment : Fragment(), MainContract.View {

private lateinit var earthquakesAdapter: EarthquakeRecyclerViewAdapter
private lateinit var earthquakes: EarthquakeRootObject
@Inject lateinit var presenter: MainContract.Presenter

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    return container?.inflate(R.layout.fragment_main)
}

override fun onAttach(context: Context?) {
    super.onAttach(context)
    MainApp.graph.plus(MainModule(this)).inject(this)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    earthquakesRecyclerView.layoutManager = LinearLayoutManager(context)
    earthquakesRecyclerView.setHasFixedSize(true)

    presenter.loadData()
}

我的主持人

class MainPresenter (var view: MainContract.View?) : MainContract.Presenter {

private var disposable: Disposable? = null
@Inject lateinit var api : EarthquakeApi

override fun onDestroy() {
    disposable?.dispose()
    view = null
}

override fun loadData() {
    view?.showLoading()

    disposable = api.getEarthquakes()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    {
                        response ->
                        view?.showData(response)
                        view?.hideLoading()
                    },
                    {
                        throwable ->
                        view?.showMessage(throwable.message ?: "Ошибка")
                        view?.hideLoading()
                    }
            )
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要更换

fun inject (view : MainContract.View)

使用

fun inject(target : MainFragment)