Android 获得;没有虚拟方法getTag(Ljava/lang/String;”);使用viewModelScope.launch在ViewModel内部调用挂起的方法时出错
我正试图通过Android 获得;没有虚拟方法getTag(Ljava/lang/String;”);使用viewModelScope.launch在ViewModel内部调用挂起的方法时出错,android,kotlin,androidx,android-viewmodel,kotlin-coroutines,Android,Kotlin,Androidx,Android Viewmodel,Kotlin Coroutines,我正试图通过viewModelScope.launch()调用一个挂起的方法,但一旦调用该方法,应用程序就会崩溃 我应该指出,我正在使用最新版本的androidX稳定库。 但是,对于ViewModel和LiveData,我使用的是alpha版本,即2.2.0-alpha02。 当然,改装版本是2.6.0,因此可以将其功能标记为暂停 此外,使用Koin初始化变量 日志: 改造web服务: interface BankCalculatorWebService { @GET("auto/ban
viewModelScope.launch()
调用一个挂起的方法,但一旦调用该方法,应用程序就会崩溃
我应该指出,我正在使用最新版本的androidX稳定库。
但是,对于ViewModel和LiveData,我使用的是alpha版本,即2.2.0-alpha02
。
当然,改装版本是2.6.0
,因此可以将其功能标记为暂停
此外,使用Koin初始化变量
日志:
改造web服务:
interface BankCalculatorWebService {
@GET("auto/bank-gateway/general/{bank}/currencies")
suspend fun fetchCurrencyRates(@Path("bank") bank: String): Response<CurrencyRateResponse>
}
我也有同样的问题。除了自己实现viewModelScope之外,我找不到任何解决方案。您的HomeViewModel代码如下所示
class HomeViewModel(
private val repository: BankCalculatorRepository
) : ViewModel() {
private val viewModelJob = SupervisorJob()
protected val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
val rates = MutableLiveData<List<CurrencyRateDTO>>()
fun fetchRates() {
uiScope.launch {
val response = repository.fetchRates()
rates.postValue(response)
}
}
/**
* Cancel all coroutines when the ViewModel is cleared
*/
override fun onCleared() {
super.onCleared()
uiScope.coroutineContext.cancelChildren()
}
}
类HomeViewModel(
专用val存储库:BankCalculator存储库
):ViewModel(){
private val viewModelJob=SupervisorJob()
受保护的val uiScope=CoroutineScope(Dispatchers.Main+viewModelJob)
val rates=MutableLiveData()
利率{
发射{
val response=repository.fetchRates()
费率。后值(响应)
}
}
/**
*清除ViewModel后取消所有协同路线
*/
覆盖有趣的onCleared(){
super.onCleared()
uiScope.coroutineContext.cancelChildren()文件
}
}
这是由于对不同版本的androidx.lifecycle.ViewModel
类的依赖性污染了类路径(可能是2.2.0和2.0.0版本)。这通常是由于过时库的可传递依赖关系造成的。对我来说,是support-v4库导致了这种情况
运行/gradlew app:dependencies
查看树,然后在ViewModel
上查找可传递的依赖项。对于正在使用旧版本的库,请更新它们或排除它们的ViewModel
依赖项:
implemenation ("com.<library>") {
exclude group:'androidx.lifecycle', module:'lifecycle-viewmodel'
}
实现(“com”){
排除组:'androidx.lifecycle',模块:'lifecycle-viewmodel'
}
整个jetpack的版本不匹配libraries@EpicPandaForce是的,看到我的答案了吗
class HomeViewModel(
private val repository: BankCalculatorRepository
) : ViewModel() {
val rates = MutableLiveData<List<CurrencyRateDTO>>()
fun fetchRates() {
viewModelScope.launch {
val response = repository.fetchRates()
rates.postValue(response)
}
}
}
viewModel.rates.observe(this) {
Toast.makeText(this, it.size, Toast.LENGTH_SHORT).show()
}
button.setOnClickListener {
viewModel.fetchRates()
}
class HomeViewModel(
private val repository: BankCalculatorRepository
) : ViewModel() {
private val viewModelJob = SupervisorJob()
protected val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
val rates = MutableLiveData<List<CurrencyRateDTO>>()
fun fetchRates() {
uiScope.launch {
val response = repository.fetchRates()
rates.postValue(response)
}
}
/**
* Cancel all coroutines when the ViewModel is cleared
*/
override fun onCleared() {
super.onCleared()
uiScope.coroutineContext.cancelChildren()
}
}
implemenation ("com.<library>") {
exclude group:'androidx.lifecycle', module:'lifecycle-viewmodel'
}