「kotlin対応 はじめてのAndroid Studioプログラミング 第3版」
という書籍で、目覚まし時計を作る例題で勉強してます。
当方の環境は以下の通りです。
Android Studio3.1.3
Windows7 pro
Context.toast()が認識できません。
toastの部分が赤字になりビルドできません。
kotolin
1 2package com.example.administrator.alarmapp 3 4import android.content.BroadcastReceiver 5import android.content.Context 6import android.content.Intent 7 8import android.widget.Toast 9 10class AlarmBroadcastReceiver: BroadcastReceiver() { 11 override fun onReceive(context: Context?, intent: Intent?) { 12 13 //↓でUnresolved reference: toastとなる 14 context?.toast("アラーム受信しました") 15 } 16} 17
参考書には、Contextクラスの拡張関数として実装されています。
という説明だけです。
どうすればtoast()を使えるようになるでしょうか?
分かる方教えていただけないでしょうか?
宜しくお願い致します。
2018/09/19 追記
Ankoへの参照がうまくいってないのでしょうか?
gradleのコードを追記します。
\AlarmApp\build.gradle
※ この1行を追加しました→ext.anko_version = '0.10.4'
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.61' 5 ext.anko_version = '0.10.4' //これを追加 6 repositories { 7 google() 8 jcenter() 9 } 10 dependencies { 11 classpath 'com.android.tools.build:gradle:3.1.3' 12 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 14 // NOTE: Do not place your application dependencies here; they belong 15 // in the individual module build.gradle files 16 } 17} 18 19allprojects { 20 repositories { 21 google() 22 jcenter() 23 } 24} 25 26task clean(type: Delete) { 27 delete rootProject.buildDir 28} 29
AlarmApp\app\build.gradle
※この1行を追加しました→implementation "org.jetbrains.anko:anko-commons:$anko_version"
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 applicationId "com.example.administrator.alarmapp" 11 minSdkVersion 21 12 targetSdkVersion 28 13 versionCode 1 14 versionName "1.0" 15 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 } 17 buildTypes { 18 release { 19 minifyEnabled false 20 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 } 22 } 23} 24 25dependencies { 26 implementation "org.jetbrains.anko:anko-commons:$anko_version" //これを追加 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
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/19 06:12
2018/09/19 07:21
2018/09/19 09:12 編集