前提・実現したいこと
はじめてのAndroidプログラミング 第4版
Chapter11:My Alarm Clockで下記エラー解決できず困っています。
発生している問題・エラーメッセージ
com/example/kenta/myalarmclock/MainActivity.kt Unresolved reference: OnDateSelectedListener 'onSelected' overrides nothing Unresolved reference: format 'onPositiveClick' overrides nothing Unresolved reference: requestDimissKeyguard Unresolved reference: FLAG_TURN_SCREEN_ON Unresolved reference: FLAG_SHOW_WHEN_LOCKED Unresolved reference: FLAG_TURN_SCREEN_ON Unresolved reference: FLAG_SHOW_WHEN_LOCKED Unresolved reference: FLAG_DISMISS_KEYGUARD > Task :app:buildInfoGeneratorDebug
該当のソースコード
Kotlin
1 2package com.example.kenta.myalarmclock 3 4import android.app.AlarmManager 5import android.app.KeyguardManager 6import android.app.PendingIntent 7import android.content.Context 8import android.content.Intent 9import android.icu.text.SimpleDateFormat 10import android.os.Build 11import androidx.appcompat.app.AppCompatActivity 12import android.os.Bundle 13import android.provider.ContactsContract 14import android.widget.Toast 15import kotlinx.android.synthetic.main.activity_main.* 16import java.lang.IllegalArgumentException 17import java.text.DateFormat 18import java.text.ParseException 19import java.util.* 20import java.util.regex.Pattern 21 22class MainActivity : AppCompatActivity(), TimeAlertDialog.Listener 23 , DatePickerFragment.OnDateSelectedListener 24 , TimePickerFragment.OnTimeSelectedListener { 25 26 override fun getUp() { 27 finish() 28 } 29 30 override fun onSelected(year: Int, month: Int, date: Int) { 31 val c = Calendar.getInstance() 32 c.set(year, month, date) 33 dateText.text = DateFormat.format("yyyy/MM/dd", c) 34 } 35 36 override fun onSelected(hourOfDay: Int, minute: Int) { 37 timeText.text = "%1$02d:%2$02d".format(hourOfDay, minute) 38 } 39 40 override fun onPositiveClick() { 41 finish() 42 } 43 44 override fun snooze() { 45 val calendar = Calendar.getInstance() 46 calendar.timeInMillis = System.currentTimeMillis() 47 calendar.add(Calendar.MINUTE, 5) 48 setAlarmManager(calendar) 49 finish() 50 } 51 52 override fun onCreate(savedInstanceState: Bundle?) { 53 super.onCreate(savedInstanceState) 54 55 if (intent?.getBooleanExtra("onReceive", false) == true) { 56 when { 57 Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 -> { 58 setShowWhenLocked(true) 59 setTurnScreenOn(true) 60 val keyguardManager = 61 getSystemService(KEYGUARD_SERVICE) as KeyguardManager 62 keyguardManager.requestDimissKeyguard(this, null) 63 } 64 Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { 65 window.addFlags( 66 FLAG_TURN_SCREEN_ON or FLAG_SHOW_WHEN_LOCKED 67 ) 68 val keyguardManager = 69 getSystemService(KEYGUARD_SERVICE) as KeyguardManager 70 keyguardManager.requestDismissKeyguard(this, null) 71 } 72 else -> 73 window.addFlags( 74 FLAG_TURN_SCREEN_ON or 75 FLAG_SHOW_WHEN_LOCKED or FLAG_DISMISS_KEYGUARD 76 ) 77 } 78 79 val dialog = TimeAlertDialog() 80 dialog.show(supportFragmentManager, "alert_dialog") 81 } 82 83 setContentView(R.layout.activity_main) 84 85 setAlarm.setOnClickListener { 86 val date = "${dateText.text} ${timeText.text}".toDate() 87 when { 88 date != null -> { 89 val calendar = Calendar.getInstance() 90 calendar.time = date 91 setAlarmManager(calendar) 92 Toast.makeText( 93 this, "アラームをセットしました", 94 Toast.LENGTH_SHORT 95 ).show() 96 } 97 else -> { 98 Toast.makeText( 99 this, "日付の形式が正しくありません", 100 Toast.LENGTH_SHORT 101 ).show() 102 } 103 } 104 105 cancelAlarm.setOnClickListener { 106 cancelAlarmManager() 107 } 108 109 dateText.setOnClickListener { 110 val dialog = DatePickerFragment() 111 dialog.show(supportFragmentManager, "date_dialog") 112 } 113 114 timeText.setOnClickListener { 115 val dialog = TimePickerFragment() 116 dialog.show(supportFragmentManager, "time_dialog") 117 } 118 } 119 } 120 121 private fun setAlarmManager(calendar: Calendar) { 122 val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager 123 val intent = Intent(this, AlarmBroadcastReceiver::class.java) 124 val pending = PendingIntent.getBroadcast(this, 0, intent, 0) 125 when { 126 Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> { 127 val info = AlarmManager.AlarmClockInfo( 128 calendar.timeInMillis, null 129 ) 130 am.setAlarmClock(info, pending) 131 } 132 Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT -> { 133 am.setExact( 134 AlarmManager.RTC_WAKEUP, 135 calendar.timeInMillis, pending 136 ) 137 } 138 else -> { 139 am.set( 140 AlarmManager.RTC_WAKEUP, 141 calendar.timeInMillis, pending 142 ) 143 } 144 } 145 } 146 147 private fun cancelAlarmManager() { 148 val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager 149 val intent = Intent(this, AlarmBroadcastReceiver::class.java) 150 val pending = PendingIntent.getBroadcast(this, 0, intent, 0) 151 am.cancel(pending) 152 } 153 154 private fun String.toDate(pattern: String = "yyyy/MM/dd"): Date? { 155 return try { 156 SimpleDateFormat(pattern).parse(this) 157 } catch (e: IllegalArgumentException) { 158 return null 159 } catch (e: ParseException) { 160 return null 161 } 162 } 163} 164
試したこと
ネットで複数Unresolved referenceの解決方法について調べましたが解決できずでした。
初心者のため、深く理解できていない可能性は高いです。
補足情報(FW/ツールのバージョンなど)
Mac(macOS Mojave:10.14.4)
AndroidStudio3.4を利用しております。
他に必要な情報などあればお教えいただければ調べて記載いたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/11 03:21
2019/05/11 03:34
2019/05/11 09:03 編集
2019/05/11 09:11 編集
2019/05/18 03:12