Failure in ArrayList map in Android


Kotlin  0.4.317 at runtime


  val list = ArrayList<Pair<String,String>>()
  list.add(Pair(“Sample”, “”))
  list.add(Pair(“Scripting”, “”))

  &nbsp;&nbsp;val keys = { it.first }.toArray&lt;String&gt;()

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silverkeytech.android_rivers/com.silverkeytech.android_rivers.TryOutActivity}: java.lang.ClassCastException: [Ljava.lang.Object;   at   at   at$1500(   at$H.handleMessage(   at android.os.Handler.dispatchMessage(   at android.os.Looper.loop(   at   at java.lang.reflect.Method.invokeNative(Native Method)   at java.lang.reflect.Method.invoke(   at$   at   at dalvik.system.NativeStart.main(Native Method)   Caused by: java.lang.ClassCastException: [Ljava.lang.Object;   at com.silverkeytech.android_rivers.TryOutActivity.handleDownloadRss(TryOutActivity.kt:85)   at com.silverkeytech.android_rivers.TryOutActivity.onCreate(TryOutActivity.kt:64)   at   at   ... 11 more


It looks like the toArray<String>() breaks in Android 2.2


Seems like a general bug. Two working alternatives:


  val keys = { it.first }.toArray(array<String>())

  val keys = { it.first }.toArray() // type is Array<Any?>, so you need to cast every element


Thanks. I just filed the bug here