I have this bizarre problem with this function. When I target SDK 28, no problem and no crash happen. Here's the working code with target 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. So I upgraded to 29 SDK, which causes compile error with the previous function, so I fixed it to the following one:
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:
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:
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:
val itemsWithSubCats: MutableList<in Any?> = mutableListOf(items)
if (subCategories?.isNotEmpty() == true) {
itemsWithSubCats.add(subCategories!!)
}
val list = itemsWithSubCats.filterNotNull().toList()
In Android Studio, line
itemsWithSubCats.add(ArrayList<Any>(subCategories!!))
is clearly giving error on my side.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.