如何解决org.kodein.di.Kodein $ NotFoundException:未找到具有<LoginFragment>的bind <LLApi>的绑定 {? }
我正在使用objectBox +改造+ kodein进行项目。 但是我遇到了这个错误。
org.kodein.di.Kodein $ NotFoundException:未找到带有?()的bind()的绑定。 {? }
KodeinApplication.kt
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.js"></script>
<div id="app">
<v-app>
<v-container fluid>
<v-autocomplete :items="countries" label="Country" placeholder="Country" outlined dense></v-autocomplete>
<v-select :items="coins" label="Coin" placeholder="Coin" outlined dense></v-select>
</v-container>
</v-app>
</div>
}
BaseRepository.kt
class KodeinApplication : Application(),KodeinAware {
override val kodein = Kodein.lazy {
import(androidXModule(this@KodeinApplication))
bind() from singleton { AppVersion }
bind() from singleton { NetworkConnectivityInterceptor(instance()) }
bind() from singleton { LLApi }
bind() from singleton { LoginRepository(instance(),instance()) }
bind() from provider { LoginviewmodelFactory(instance()) }
}
){
抽象乐趣loadData():LiveData
内联乐趣fetchData(跨内联调用:(LLApi)-> Deferred
abstract class BaseRepository<T>(
val service: LLApi,@PublishedApi internal val BoxStore: BoxStore
}
LoginRepository.Kt
Coroutinescope(dispatchers.IO).launch {
val request = call(service)
withContext(dispatchers.Main) {
try {
val response = request.await()
if (response.isSuccessful) {
result.value = response.body()
} else {
Timber.d("Error occurred with code ${response.code()}")
}
} catch (e: HttpException) {
Timber.d("Error: ${e.message()}")
} catch (e: Throwable) {
Timber.d("Error: ${e.message}")
}
}
}
return result
}
inline fun <reified T : Any> savetoDatabase(data: T) {
Coroutinescope(dispatchers.IO).launch {
BoxStore.BoxFor<T>().removeAll() // deleting and inserting data to avoid sync issues
BoxStore.BoxFor<T>().put(data)
}
}
}
LoginviewmodelFactory.kt
class LoginRepository(service: LLApi,store: BoxStore) :
BaseRepository<UserInfo>(service,store) {
private var loginRequest: LoginRequest? = null
init {
this.loginRequest = loginRequest
}
override fun loadData(): LiveData<UserInfo> {
return fetchData { service.userLogin(loginRequest) }
}
}
Loginviewmodel.kt
class LoginviewmodelFactory(private val repository: LoginRepository) :
viewmodelProvider.NewInstanceFactory() {
override fun <T : viewmodel?> create(modelClass: Class<T>): T {
return Loginviewmodel(repository) as T
}
}
LoginRequest.kt
class Loginviewmodel(val repository: LoginRepository) : Baseviewmodel<UserInfo>() {
private lateinit var user: LiveData<UserInfo>
override fun getDataFromService(): LiveData<UserInfo> {
user = repository.loadData()
return user
}
override fun savetoDatabase(data: UserInfo) {
repository.savetoDatabase(data)
}
)
LoginFragment.kt
data class LoginRequest(
@Serializedname("mobile_no") val mobileNumber:String?,val password:String?
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。