現在kotlinでjsonを扱いたく勉強中です。
やりたい事は
assetsファイルを作成してその中にjsonファイルを作りました。そのjsonの中の一つの値をAndroid画面に取り出したいと思っています。
コードにエラーは見られないのですが、いざ、アプリを起動すると下記のエラーが出てしまいます。これが解決できません。
どなたかご教授よろしくお願いいたします。
エラー内容
e: /Users/~~~~~/myapplication/MainActivity.kt: (25, 1): Your current kotlinx.serialization core version is too low, while current Kotlin compiler plugin 1.6.21 requires at least 1.0-M1-SNAPSHOT. Please update your kotlinx.serialization runtime dependency.
MainActivity.kt
1 2import androidx.appcompat.app.AppCompatActivity 3import android.os.Bundle 4import kotlinx.serialization.Serializable 5import android.content.res.Resources 6import android.widget.TextView 7import kotlinx.serialization.builtins.list 8import kotlinx.serialization.json.Json 9import kotlinx.serialization.json.JsonConfiguration 10import java.io.BufferedReader 11import java.io.InputStreamReader 12 13class MainActivity : AppCompatActivity() { 14 override fun onCreate(savedInstanceState: Bundle?) { 15 super.onCreate(savedInstanceState) 16 setContentView(R.layout.activity_main) 17 val TV = findViewById<TextView>(R.id.TV) 18 19 val sights = getSights(resources) 20 TV.text = sights[0].kind 21 } 22} 23@Serializable 24class Sight ( 25 val name : String, 26 val imageName : String, 27 val description : String, 28 val kind : String 29 30 ) 31 32 33fun getSights(resources: Resources): List<Sight> { 34 val assetManager = resources.assets 35 val inputStream = assetManager.open("sights.json") 36 val bufferedReader = BufferedReader(InputStreamReader(inputStream)) 37 val str: String = bufferedReader.readText() 38 val obj = Json(JsonConfiguration.Stable) 39 .parse(Sight.serializer().list, str) 40 return obj 41} 42 43 44
assets/sights.json
1[ 2 { 3 "name": "アンコール・ワット", 4 "imageName": "angkor", 5 "description": "アンコール・ワットは、カンボジア北西部に位置するユネスコの世界遺産であるアンコール遺跡の一つであり、その遺跡群を代表する寺院。ヒンドゥー教寺院として作られたが、16世紀後半に仏教寺院に改修され、現在も上座部仏教寺院となっている。", 6 "kind": "文化遺産" 7 }, 8 { 9 "name": "ホースシューベンド", 10 "imageName": "horseshoebend", 11 "description": "ホースシューベンド(Horseshoe Bend)とは、アメリカ合衆国アリゾナ州ページの町付近にある、コロラド川が蹄鉄(horseshoe)の形に穿入蛇行している場所の名前である。グレンキャニオンダムとパウエル湖から少し下流、ページの南約6キロメートルに位置している。国道89号線から1.2キロメートル歩くとたどり着ける。", 12 "kind": "自然・公園" 13 } 14] 15
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/TV" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Hello World!" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintLeft_toLeftOf="parent" 16 app:layout_constraintRight_toRightOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18 19</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle(Project:
1buildscript { 2 ext.kotlin_version = "1.6.21" 3 repositories { jcenter() } 4 5 dependencies { 6 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 7 classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" 8 } 9} 10 11plugins { 12 id 'com.android.application' version '7.1.2' apply false 13 id 'com.android.library' version '7.1.2' apply false 14 id 'org.jetbrains.kotlin.android' version '1.6.21' apply false 15} 16 17task clean(type: Delete) { 18 delete rootProject.buildDir 19} 20
build.gradle(Module:
1plugins { 2 id 'com.android.application' 3 id 'org.jetbrains.kotlin.android' 4 id 'kotlinx-serialization' 5} 6 7android { 8 compileSdk 32 9 10 defaultConfig { 11 applicationId "com.example.myapplication" 12 minSdk 21 13 targetSdk 32 14 versionCode 1 15 versionName "1.0" 16 17 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 } 19 20 buildTypes { 21 release { 22 minifyEnabled false 23 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 24 } 25 } 26 compileOptions { 27 sourceCompatibility JavaVersion.VERSION_1_8 28 targetCompatibility JavaVersion.VERSION_1_8 29 } 30 kotlinOptions { 31 jvmTarget = '1.8' 32 } 33} 34 35dependencies { 36 37 implementation 'androidx.core:core-ktx:1.7.0' 38 implementation 'androidx.appcompat:appcompat:1.4.1' 39 implementation 'com.google.android.material:material:1.6.0' 40 implementation 'androidx.constraintlayout:constraintlayout:2.1.3' 41 testImplementation 'junit:junit:4.13.2' 42 androidTestImplementation 'androidx.test.ext:junit:1.1.3' 43 androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 44 45 implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0" 46} 47

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/18 09:56
2022/05/19 15:08