我正在用 objectbox + retrofit + kodein 做一个项目。但我收到了这个错误。
org.kodein.di.Kodein$NotFoundException:没有找到绑定()与?()的绑定。?{ ? }
KodeinApplication.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()) }
}
}
BaseRepository.kt
abstract class BaseRepository<T>(
val service: LLApi,
@PublishedApi internal val boxStore: BoxStore
) { abstract fun loadData(): LiveData inline fun fetchData(crossinline call: (LLApi) -> Deferred<Response>): LiveData { val result = MutableLiveData()
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)
}
}
}
登录存储库.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) }
}
}
登录视图模型工厂.kt
class LoginViewModelFactory(private val repository: LoginRepository) :
ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return LoginViewModel(repository) as T
}
}
登录视图模型.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)
}
}
登录请求.kt
data class LoginRequest(
@SerializedName("mobile_no") val mobileNumber:String?,
val password:String?
)
登录片段.kt
class LoginFragment() : Fragment(), KodeinAware {
override val kodein by kodein()
private val factory: LoginViewModelFactory by instance()
private lateinit var binding: LoginFragmentBinding
private lateinit var viewModel: LoginViewModel
private var emailOrPhone: EditText? = null
private var password: EditText? = null
companion object {
fun newInstance(): LoginFragment {
return LoginFragment()
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.setContentView(requireActivity(), R.layout.login_fragment)
viewModel = ViewModelProvider(
this, factory
).get(LoginViewModel::class.java)
val view = inflater.inflate(R.layout.login_fragment, container, false)
emailOrPhone = view?.findViewById(R.id.edt_number_or_email)
password = view?.findViewById(R.id.edt_password)
return view
}
fun userLoginService() {
val email = binding.edtNumberOrEmail.text.toString()
val password = binding.edtPassword.text.toString()
val liveData = viewModel.getDataFromService()
liveData.observe(this, Observer {
if (it != null) {
viewModel.saveTODatabase(it)
}
})
}
}
请随时询问所需的任何文件,并请帮助我解决这个问题..