Failure in ArrayList map in Android


#1

Kotlin  0.4.317 at runtime

``


  val list = ArrayList<Pair<String,String>>()
  list.add(Pair(“Sample”, “http://cyber.law.harvard.edu/rss/examples/rss2sample.xml”))
  list.add(Pair(“Scripting”, “http://static.scripting.com/rss.xml”))

  &nbsp;&nbsp;val keys = list.map { 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)   at android.app.ActivityThread.access$1500(ActivityThread.java:117)   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)   at android.os.Handler.dispatchMessage(Handler.java:99)   at android.os.Looper.loop(Looper.java:130)   at android.app.ActivityThread.main(ActivityThread.java:3683)   at java.lang.reflect.Method.invokeNative(Native Method)   at java.lang.reflect.Method.invoke(Method.java:507)   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)   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 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)   ... 11 more


#2

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


#3

Seems like a general bug. Two working alternatives:

``

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

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

#4

Thanks. I just filed the bug here http://youtrack.jetbrains.com/issue/KT-3177