Injeção de dependência no Kotlin com Koin
Alex Felipe
1281

Fala alex, tudo bem? estou tendo um problema acompanhando seu artigo.

Aparece a seguinte mensagem de erro:

kotlin.UninitializedPropertyAccessException: lateinit property koinContext has not been initialized

O erro está na linha em que crio o live products list

val productsLiveData = productDao.all()

Vou encaminhar com mais detalhes o stack trace para que vc possa dar uma mão hehe

¹ Stack Trace

E/AndroidRuntime: FATAL EXCEPTION: main
Process: alexf.com.br.techstore, PID: 27914
java.lang.RuntimeException: Unable to start activity ComponentInfo{alexf.com.br.techstore/alexf.com.br.techstore.ui.activity.ProductsListActivity}: kotlin.UninitializedPropertyAccessException: lateinit property koinContext has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property koinContext has not been initialized
at org.koin.standalone.StandAloneContext.getKoinContext(StandAloneContext.kt:48)
at org.koin.android.ext.android.ComponentCallbacksExtKt.getKoin(ComponentCallbacksExt.kt:127)
at alexf.com.br.techstore.ui.activity.ProductsListActivity$$special$$inlined$inject$1.invoke(ComponentCallbacksExt.kt:147)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at alexf.com.br.techstore.ui.activity.ProductsListActivity.getProductDao(ProductsListActivity.kt)
at alexf.com.br.techstore.ui.activity.ProductsListActivity.onCreate(ProductsListActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:6754)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679

¹Lista de Produtos Activity

package alexf.com.br.techstore.ui.activity

import alexf.com.br.techstore.R
import alexf.com.br.techstore.database.dao.ProductDao
import alexf.com.br.techstore.ui.activity.recyclerview.ProductsListAdapter
import android.arch.lifecycle.Observer
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_products_list.*
import org.koin.android.ext.android.inject


class ProductsListActivity : AppCompatActivity() {

private val productDao: ProductDao by inject()
private val adapter: ProductsListAdapter by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_products_list)
val productsLiveData = productDao.all()
productsLiveData.observe(this, Observer { products ->
products?.let {
adapter.replaceAllProducts(it)
}
}
)
configureRecyclerView()
configureFabAddProduct()
}

private fun configureFabAddProduct() {
product_list_add_product.setOnClickListener {
val openProductForm = Intent(this, FormProductActivity::class.java)
startActivity(openProductForm)
}
}

private fun configureRecyclerView() {
products_list_recyclerview.adapter = adapter
}

}

Do resto está tudo exatamente igual, os modulos separados, o Application dando start no koin

tem como dar uma mão?