[英]ClassCastException when upgrading SDK from 28 to 29
I have this bizarre problem with this function.我对这个 function 有这个奇怪的问题。 When I target SDK 28, no problem and no crash happen.当我以 SDK 28 为目标时,没有问题,也没有发生崩溃。 Here's the working code with target SDK 28.这是目标 SDK 28 的工作代码。
override fun getChildList(): List<Any> {
val itemsWithSubCats: MutableList<in Any> = ArrayList(items)
if (subCategories?.isNotEmpty() == true) {
itemsWithSubCats.add(ArrayList<Any>(subCategories))
}
return itemsWithSubCats.toList() as List<Any>
}
I've uploaded a release build to Playstore and I got an upgrade SDK problem.我已将发布版本上传到 Playstore,但遇到了升级 SDK 问题。 So I upgraded to 29 SDK, which causes compile error with the previous function, so I fixed it to the following one:于是我升级到了29个SDK,导致与之前的function编译出错,所以我将其修复为以下一个:
override fun getChildList(): List<Any> {
val itemsWithSubCats: MutableList<in Any?> = mutableListOf(items)
if (subCategories?.isNotEmpty() == true) {
itemsWithSubCats.add(ArrayList<Any>(subCategories!!))
}
return itemsWithSubCats.filterNotNull().toList()
}
which causes a ClassCastException as follow:这会导致 ClassCastException 如下:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to ..RestaurantDetailsMenuItem
Assumpsions假设
items = arrayOf("a", "b")
subCategories = arrayOf("c", "d")
If you want to achieve itemsWithSubCats = listOf("a", "b", "c", "d"), then the code should be like:如果要实现 itemsWithSubCats = listOf("a", "b", "c", "d"),那么代码应该是这样的:
val itemsWithSubCats: MutableList<in Any?> = items.toMutableList()
if (subCategories?.isNotEmpty() == true) {
itemsWithSubCats.addAll(subCategories!!)
}
val list = itemsWithSubCats.filterNotNull().toList()
If you want to achieve itemsWithSubCats = listOf(arrayOf("a", "b"), arrayOf("c", "d")), then the code should be like:如果要实现 itemsWithSubCats = listOf(arrayOf("a", "b"), arrayOf("c", "d")),那么代码应该是这样的:
val itemsWithSubCats: MutableList<in Any?> = mutableListOf(items)
if (subCategories?.isNotEmpty() == true) {
itemsWithSubCats.add(subCategories!!)
}
val list = itemsWithSubCats.filterNotNull().toList()
In Android Studio, line在 Android Studio 中,行
itemsWithSubCats.add(ArrayList<Any>(subCategories!!))
is clearly giving error on my side.显然在我这边给出错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.