Trying to use Dagger with Kotlin on Android. And got the exception:
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 ...
The app is special for testing. I get earthquakes and show them on a device screen.
All I want is 1. Inject the presenter in my Fragment (View) 2. Inject the api in my presenter
There is the structure of my project:
There are two modules and components there, as you can see:
@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)
}
}
And components:
@Component(modules = [ApplicationModule::class])
interface ApplicationComponent {
fun inject(application: Application)
fun plus (mainModule: MainModule) : MainComponent
}
and
@Subcomponent(modules = [MainModule::class])
interface MainComponent {
fun inject (view : MainContract.View)
}
There is a contract:
interface MainContract {
interface View {
fun showLoading()
fun hideLoading()
fun showMessage(message: String)
fun showData(data: EarthquakeRootObject)
}
interface Presenter {
fun onDestroy()
fun loadData()
}
}
Application class:
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()
}
}
Fragment (where I want to use Injection)
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()
}
And my presenter
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()
}
)
}
What do I do wrong?