Android 类型不匹配:推断类型为除列表以外的任何类型<;SportNewsResponse>;?预料之中
我正在开发新闻应用程序,我在MainViewModel.kt类中遇到以下错误 类型不匹配:推断的类型是除列表之外的任何类型?预料之中 在我的MainViewModel.kt下面Android 类型不匹配:推断类型为除列表以外的任何类型<;SportNewsResponse>;?预料之中,android,kotlin,inferred-type,Android,Kotlin,Inferred Type,我正在开发新闻应用程序,我在MainViewModel.kt类中遇到以下错误 类型不匹配:推断的类型是除列表之外的任何类型?预料之中 在我的MainViewModel.kt下面 class MainViewModel( private val sportNewsInterface: SportNewsInterface ) : ViewModel(), CoroutineScope { // Coroutine's background job private val
class MainViewModel(
private val sportNewsInterface: SportNewsInterface
) : ViewModel(), CoroutineScope {
// Coroutine's background job
private val job = Job()
// Define default thread for Coroutine as Main and add job
override val coroutineContext: CoroutineContext = Dispatchers.Main + job
private val showLoading = MutableLiveData<Boolean>()
private val sportList = MutableLiveData <List<SportNewsResponse>>()
val showError = SingleLiveEvent<String>()
fun loadNews() {
// Show progressBar during the operation on the MAIN (default) thread
showLoading.value = true
// launch the Coroutine
launch {
// Switching from MAIN to IO thread for API operation
// Update our data list with the new one from API
val result = withContext(Dispatchers.IO) { sportNewsInterface.getNews()
}
// Hide progressBar once the operation is done on the MAIN (default) thread
showLoading.value = false
when (result) {
is UseCaseResult.Success<*> -> {
sportList.value = result.data
}
is UseCaseResult.Error -> showError.value = result.exception.message
}
}
}
override fun onCleared() {
super.onCleared()
// Clear our job when the linked activity is destroyed to avoid memory leaks
job.cancel()
}
}
class主视图模型(
专用val sportNewsInterface:sportNewsInterface
):ViewModel(),CoroutineScope{
//Coroutine的背景工作
private val job=job()
//将协同程序的默认线程定义为主线程并添加作业
override val coroutineContext:coroutineContext=Dispatchers.Main+job
private val showLoading=MutableLiveData()
private val sportList=MutableLiveData()
val-showError=SingleLiveEvent()
有趣的新闻{
//在主(默认)线程上的操作期间显示progressBar
showLoading.value=true
//启动协同程序
发射{
//从主线程切换到IO线程以进行API操作
//用API中的新数据更新我们的数据列表
val result=withContext(Dispatchers.IO){sportNewsInterface.getNews()
}
//在主(默认)线程上完成操作后隐藏progressBar
showLoading.value=false
何时(结果){
是UseCaseResult。成功->{
sportList.value=结果.data
}
是UseCaseResult.Error->showError.value=result.exception.message
}
}
}
覆盖有趣的onCleared(){
super.onCleared()
//当链接的活动被破坏时清除我们的作业,以避免内存泄漏
作业。取消()
}
}
下面是usercasesult.kt
sealed class UseCaseResult<out T : Any> {
class Success<out T : Any>(val data: T) : UseCaseResult<List<SportNewsResponse>>()
class Error(val exception: Throwable) : UseCaseResult<Nothing>()
}
密封类UseCaseResult{
类成功(val数据:T):UseCaseResult()
类错误(val异常:Throwable):UseCaseResult()
}
看看你的成功
课程。您正在使用泛型类型T
定义val数据
,其上限为Any
。在when
块中,当您想检查它是否是UseCaseResult.Success
的实例时,您使用的是一个星形投影,它会导致编译器推断结果的上限:UseCaseResult.Success
。因此,result.data
的类型将被推断为Any
解决办法是:
- 如果
只有列表类型的数据,则应省略泛型类型(当前在usecasesult.Success
类的定义中,泛型类型没有意义):Success
或:类成功(val数据:列表):UseCaseResult()
类成功(val数据:列表):UseCaseResult()
- 或者您可以简单地将when子句更改为:
is UseCaseResult.Success<List<SportNewsResponse>> -> { sportList.value = result.data }
是UseCaseResult.Success->{ sportList.value=结果.data }
成功
课程。您正在使用泛型类型T
定义val数据
,其上限为Any
。在when
块中,当您想检查它是否是UseCaseResult.Success
的实例时,您使用的是一个星形投影,它会导致编译器推断结果的上限:UseCaseResult.Success
。因此,result.data
的类型将被推断为Any
解决办法是:
- 如果
只有列表类型的数据,则应省略泛型类型(当前在usecasesult.Success
类的定义中,泛型类型没有意义):Success
或:类成功(val数据:列表):UseCaseResult()
类成功(val数据:列表):UseCaseResult()
- 或者您可以简单地将when子句更改为:
is UseCaseResult.Success<List<SportNewsResponse>> -> { sportList.value = result.data }
是UseCaseResult.Success->{ sportList.value=结果.data }
is UseCaseResult.Success<List<SportNewsResponse>> -> {
sportList.value = result.data
}