我正在Kodein
为我的kotlin
项目注入依赖项。所以我有一个 ViewModel 工厂,如下所示
class ReadViewModelFactory(private val readRepository: ReadRepositoryImpl, private val activity: ReadActivity) : ViewModelProvider.NewInstanceFactory() {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val guid = activity.intent.getIntExtra(Constants.EXTRA_GUID, 0)
val pubDate = activity.intent.getStringExtra(Constants.EXTRA_DATE)
val title = activity.intent.getStringExtra(Constants.EXTRA_TITLE)
val headImage = activity.intent.getStringExtra(Constants.EXTRA_IMAGE_URL)
val kanal = activity.intent.getStringExtra(Constants.EXTRA_KANAL_READ)
val intentRead = IntentRead(guid, pubDate, title, headImage, kanal)
return ReadViewModel(readRepository, intentRead) as T
}
}
然后,在我的 Application 类中,我创建了:
class App : MultiDexApplication(), KodeinAware{
override val kodein = Kodein.lazy {
import(androidXModule(this@App))
bind() from singleton { NetworkConnectionInterceptor(instance()) }
bind() from singleton { ApiService(instance()) }
bind() from provider { ReadViewModelFactory(instance(),instance())}
}
override fun onCreate() {
super.onCreate()
}
}
如您所见,我的ReadViewModelFactory
. 但是当我运行我的应用程序崩溃发生在ReadActivity
. 什么控制台日志说
java.lang.RuntimeException: 无法启动活动 ComponentInfo{id.my.app/id.my.app.ui.read.ReadActivity}: org.kodein.di.Kodein$NotFoundException: 没有为 bind() 找到绑定? ().? { ? 在这个 Kodein 容器中注册:
我的ReadActivity
样子如下:
class ReadActivity : AppCompatActivity(), KodeinAware{
override val kodein by kodein()
private val factory : ReadViewModelFactory by instance()
.......
}
全栈跟踪
08-21 11:23:54.807 27186-27186/id.my.app E/AndroidRuntime: 致命异常: 主进程: id.my.app, PID: 27186 java.lang.RuntimeException: 无法启动活动 ComponentInfo{id. my.app/id.grid.app.ui.read.ReadActivity}:org.kodein.di.Kodein$NotFoundException:没有找到绑定()与?()的绑定。?{ ? 在这个 Kodein 容器中注册: