18
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

androidでkotlinを使ってMap#forEachを使うとNoClassDefFoundError

Posted at

Androidアプリでkotlinを使っているときに、以下のコードのように Map#forEach を使うと NoClassDefFoundError でアプリがクラッシュします。

val map = mapOf(Pair("test", "test"))
map.forEach { k, v -> Log.d(TAG, "key: $k, value: $v") }

これは基本的にはAndroidではJavaのStream APIを使用できないため、kotlin -> javaに変換し実行する際に Map#forEach がKotlinのMapではなく、JavaのMapと勘違いしてしまいます。

解決方法

val map = mapOf(Pair("test", "test"))
map.forEach { (k, v) -> Log.d(TAG, "key: $k, value: $v") }

そこで上記のコードのように k, v(k, v) としてあげることでパラメータを一つにしてあげることで、Kotlinの Map#forEach を使うことができます。

参考

java.lang.NoClassDefFoundError $$inlined$forEach$lambda$1 in Kotlin - StackOverFlow

18
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?