質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

2回答

2951閲覧

【kotlin , android studio】FloatingActionButton.setImageResource()が、unresolved referenceになり使えない

pecchan

総合スコア555

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/09/07 05:01

編集2018/09/07 05:20

「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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/09/07 05:36

エラーメッセージを全文書いてもらえますか?
pecchan

2018/09/07 06:01

Unresolved reference: setImageResource と出てます。
guest

回答2

0

ベストアンサー

Design Support Library を利用する宣言が抜けているようです。
FloatingActionButtonはこのライブラリの中に含まれます。

gradle

1implementation 'com.android.support:design:...' // バージョンは適当に合わせてください

投稿2018/09/07 07:10

編集2018/09/07 07:20
kakajika

総合スコア3131

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

keicha_hrs

2018/09/07 07:17 編集

参考までにお尋ねしたいのですが、私の手元ではdesign:28.0.0-rc02を指定すると Failed to resolve: com.android.support:design:28.0.0-rc02 になってしまいます。ですからdesign:28.0.0-rc02は未リリースではと考えていたのですが、これは私の局所的な問題なのでしょうか?
kakajika

2018/09/07 07:19

すみません、情報ありがとうございます。バージョンはご質問内のappcompat-v7のものに適当に合わせてしまったものでしたので、修正しておきます。
pecchan

2018/09/07 07:24 編集

keicha_hrs様、いえ、こちらも同じ現象でした。 kakajika様、助かりました、有難う御座います。参考書にこの辺りの記載ありませんでした(泣)
kakajika

2018/09/07 07:35

実際の書籍を見ていないのでなんとも言えませんが、そのあたりは書いておいてほしいところですね。。 一応、こちらにあるサンプルプロジェクトを見てみるとapp/build.gradleの最後に追加されていました。 https://www.sbcr.jp/support/14530.html
guest

0

setImageResource()に引数無しメソッドは存在しないのでは?イメージのリソースIDを指定する必要が有るのではないかと思いますが。

投稿2018/09/07 07:06

keicha_hrs

総合スコア6766

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pecchan

2018/09/07 07:08

すいません。コードサジェストの途中で「Unresolved reference: setImageResource」で出てこないのです。
keicha_hrs

2018/09/07 07:14

app/build.gradleに implementation 'com.android.support:design:28.0.0-rc01' の記述も必要ですね。rc02が存在しないようなので、rc01で揃える必要がありそうです。
pecchan

2018/09/07 07:20

keicha_hrs様、有難う御座います。 rc01で揃えたところsyncが通り、setImageResource()が使えました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問