前提・実現したいこと
android studioで、「はじめてのandroidプログラミング 第4版」chapter5「最強!?じゃんけんアプリ」を作っております。
発生している問題・エラーメッセージ
本の記述通り書いているのにもかかわらず、設定したIDや定数等に対して、
以下のようなUnresolved referenceというエラーが発生してしまいます。
Unresolved reference: gu Unresolved reference: it Unresolved reference: choki Unresolved reference: it Unresolved reference: pa Unresolved reference: it Unresolved reference: myHandImage Unresolved reference: gu Unresolved reference: myHandImage Unresolved reference: choki Unresolved reference: myHandImage Unresolved reference: pa Unresolved reference: gu Unresolved reference: gu Unresolved reference: comHandImage Unresolved reference: choki Unresolved reference: comHandImage Unresolved reference: pa Unresolved reference: comHandImage Unresolved reference: resultLavel Unresolved reference: resultLavel Unresolved reference: result_win Unresolved reference: resultLavel Unresolved reference: backButton
初心者のため、何が悪いのかが分からず困惑しております。
ご教授の程、どうぞよろしくお願い致します。
該当のソースコード
MainActivity
1package com.example.kotolinfight 2 3import android.content.Intent 4import android.os.Bundle 5import android.view.View 6import androidx.appcompat.app.AppCompatActivity 7 8class MainActivity : AppCompatActivity() { 9 override fun onCreate(savedInstanceState: Bundle?) { 10 super.onCreate(savedInstanceState) 11 setContentView(R.layout.activity_main) 12 13 gu.setOnClickListener { onJankenButtonTapped(it)} 14 choki.setOnClickListener { onJankenButtonTapped(it)} 15 pa.setOnClickListener { onJankenButtonTapped(it)} 16 17 } 18 19 fun onJankenButtonTapped(view: View?){ 20 val intent = Intent(this, ResultActivity::class.java) 21 intent.putExtra("MY_HAND",view?.id) 22 startActivity(intent) 23 } 24}
ResultActivity
1package com.example.kotolinfight 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5 6class ResultActivity : AppCompatActivity() { 7 override fun onCreate(savedInstanceState: Bundle?) { 8 super.onCreate(savedInstanceState) 9 setContentView(R.layout.activity_result) 10 val id=intent.getIntExtra("MY_HAND",0) 11 12 val myHand: Int 13 myHand= when(id){ 14 R.id.gu-> { 15 myHandImage.setImageResource(R.drawable.gu) 16 gu 17 } 18 R.id.choki-> { 19 myHandImage.setImageResource(R.drawable.choki) 20 choki 21 } 22 R.id.pa->{ 23 myHandImage.setImageResource(R.drawable.pa) 24 pa 25 } 26 else->gu 27 } 28 29 val comHand=(Math.random()*3).toInt() 30 when(comHand){ 31 gu->comHandImage.setImageResorce(R.drawable.com_gu) 32 choki->comHandImage.setImageResorce(R.drawable.com_choki) 33 pa->comHandImage.setImageResorce(R.drawable.com_pa) 34 } 35 36 37 val gameResult=(comHand-myHand+3)%3 38 39 when(gameResult){ 40 0->resultLavel.setText(R.string.result_draw) 41 1->resultLavel.setText(R.string.result_win) 42 2->resultLavel.setText(R.string.result_lose) 43 44 } 45 backButton.setOnClickListener{finish()} 46 } 47} 48
gradle(プロジェクト)です。↓
gradle(project)
1// Top-level build file where you can add configuration options common to all sub-projects/modules. 2buildscript { 3 ext.kotlin_version = "1.4.30" 4 repositories { 5 google() 6 jcenter() 7 } 8 dependencies { 9 def lifecycle_version = "2.2.0" 10 classpath "com.android.tools.build:gradle:4.1.2" 11 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 13 // NOTE: Do not place your application dependencies here; they belong 14 // in the individual module build.gradle files 15 } 16} 17 18allprojects { 19 repositories { 20 google() 21 jcenter() 22 } 23} 24 25task clean(type: Delete) { 26 delete rootProject.buildDir 27}
gradle(モジュール)です。↓
gradle(module)
1plugins { 2 id 'com.android.application' 3 id 'kotlin-android' 4} 5 6android { 7 compileSdkVersion 30 8 buildToolsVersion "30.0.3" 9 10 defaultConfig { 11 applicationId "com.example.kotolinfight" 12 minSdkVersion 19 13 targetSdkVersion 30 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 "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 38 implementation 'androidx.core:core-ktx:1.3.2' 39 implementation 'androidx.appcompat:appcompat:1.2.0' 40 implementation 'com.google.android.material:material:1.3.0' 41 implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 42 testImplementation 'junit:junit:4.+' 43 androidTestImplementation 'androidx.test.ext:junit:1.1.2' 44 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 45}
補足情報(FW/ツールのバージョンなど)
バージョンは、4.1.2です。