「kotlin対応 はじめてのAndroid Studioプログラミング 第3版」
という書籍の、タイマーを作る例題で勉強してます。
当方の環境は以下の通りです。
Android Studio3.1.3
Windows7 pro
FloatingActionButton.setImageResource()の箇所で
「unresolved reference」となります。
どうしても原因が分かりません。
分かる方教えて下さい。
java
1package com.example.administrator.timerapp 2 3import android.media.AudioAttributes 4import android.media.AudioManager 5import android.media.SoundPool 6import android.os.Build 7import android.os.Bundle 8import android.os.CountDownTimer 9import android.support.v7.app.AppCompatActivity 10import kotlinx.android.synthetic.main.activity_main.* 11 12 13class MainActivity : AppCompatActivity() { 14 15 // 16 inner class MyCountDownTimer(millisInFuture: Long, 17 countDownInterval: Long) : 18 CountDownTimer(millisInFuture, countDownInterval){ 19 20 var isRunning = false 21 22 override fun onTick(millisUntlFinished: Long) { 23 val minute = millisUntlFinished / 1000L / 60L 24 val second = millisUntlFinished / 1000L % 60L 25 timerText.text = "%1d:%2$02d".format(minute, second) 26 } 27 28 override fun onFinish() { 29 timerText.text = "0:00" 30 } 31 } 32 33 override fun onCreate(savedInstanceState: Bundle?) { 34 super.onCreate(savedInstanceState) 35 setContentView(R.layout.activity_main) 36 37 38 timerText.text = "3:00" 39 val timer = MyCountDownTimer(3 * 60 * 1000, 100) 40 playStop.setOnClickListener { 41 when (timer.isRunning){ 42 true -> timer.apply { 43 isRunning = false 44 cancel() 45 //ここで「unresolved reference」エラー 46 playStop.setImageResource() 47 } 48 } 49 } 50 } 51} 52
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.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 android:keepScreenOn="true" 8 tools:context=".MainActivity"> 9 10 <android.support.design.widget.FloatingActionButton 11 android:id="@+id/playStop" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_marginBottom="16dp" 15 android:clickable="true" 16 android:tint="@android:color/white" 17 app:elevation="0dp" 18 app:layout_constraintBottom_toBottomOf="parent" 19 app:layout_constraintLeft_toLeftOf="parent" 20 app:layout_constraintRight_toRightOf="parent" 21 app:srcCompat="@drawable/ic_play_arrow_black_24dp" /> 22 23 <TextView 24 android:id="@+id/timerText" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_marginBottom="8dp" 28 android:textColor="@color/colorAccent" 29 android:textSize="150sp" 30 app:layout_constraintBottom_toTopOf="@+id/playStop" 31 app:layout_constraintLeft_toLeftOf="parent" 32 app:layout_constraintRight_toRightOf="parent" 33 app:layout_constraintTop_toTopOf="parent" 34 tools:text="3:00" /> 35 36</android.support.constraint.ConstraintLayout>
//build.grade(Module: app)
java
1apply plugin: 'com.android.application' 2 3apply plugin: 'kotlin-android' 4 5apply plugin: 'kotlin-android-extensions' 6 7android { 8 compileSdkVersion 28 9 defaultConfig { 10 vectorDrawables.useSupportLibrary = true 11 applicationId "com.example.administrator.timerapp" 12 minSdkVersion 21 13 targetSdkVersion 28 14 versionCode 1 15 versionName "1.0" 16 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 17 } 18 buildTypes { 19 release { 20 minifyEnabled false 21 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 } 23 } 24} 25 26dependencies { 27 implementation fileTree(dir: 'libs', include: ['*.jar']) 28 implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 29 implementation 'com.android.support:appcompat-v7:28.0.0-rc02' 30 implementation 'com.android.support.constraint:constraint-layout:1.1.3' 31 testImplementation 'junit:junit:4.12' 32 androidTestImplementation 'com.android.support.test:runner:1.0.2' 33 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 34} 35
//build.gradle(Project)
java
1// Top-level build file where you can add configuration options common to all sub-projects/modules. 2 3buildscript { 4 ext.kotlin_version = '1.2.30' 5 repositories { 6 google() 7 jcenter() 8 } 9 dependencies { 10 classpath 'com.android.tools.build:gradle:3.1.3' 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} 28
回答2件
あなたの回答
tips
プレビュー