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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

ビルド

ソースコードを単体で実行可能なソフトウェアへ変換する過程をビルド(build)と呼びます

Kotlin

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

Q&A

1回答

1233閲覧

Unresolved reference: startButtonのエラーコードを解決したい

harehareharenoh

総合スコア0

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

ビルド

ソースコードを単体で実行可能なソフトウェアへ変換する過程をビルド(build)と呼びます

Kotlin

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

0グッド

0クリップ

投稿2021/07/26 18:17

Unresolved reference: startButtonのエラーコードを解決したい

以下のサイトを参考にフォアグラウンドサービスの実装中のKotlin勉強中の初心者です。

ビルドすると以下のようなエラーが発生します。
Unresolved reference: startButton

Main Activity.ktに定義されていないことが問題なのだと思うのですが、Activity_main.xmlにはボタンを追加して定義しています。
解決方法をご教示いただきたいです。

該当ソースコードは以下です。

Kotlin

1package com.example.fpregroundlocationupdate 2 3import android.app.NotificationChannel 4import android.app.NotificationManager 5import android.content.Context 6import android.content.Intent 7import android.content.pm.PackageManager 8import android.os.Build 9import androidx.appcompat.app.AppCompatActivity 10import android.os.Bundle 11import androidx.core.app.ActivityCompat 12import android.Manifest 13 14 15class MainActivity : AppCompatActivity() { 16 companion object{ 17 private const val PERMISSION_REQUEST_CODE =1234 18 } 19 20 override fun onCreate(savedInstanceState: Bundle?) { 21 super.onCreate(savedInstanceState) 22 setContentView(R.layout.activity_main) 23 24 requestPermission() 25 26 createNotificationChannel() 27 28 startButton.setOnClickListener { 29 val intent = Intent(this, LocationService::class.java) 30 startForegroundService(intent) 31 } 32 33 finishButton.setOnClickListener { 34 val intent = Intent(this, LocationService::class.java) 35 stopService(intent) 36 } 37 } 38 39 private fun requestPermission() { 40 val permissionAccessCoarseLocationApproved = 41 ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == 42 PackageManager.PERMISSION_GRANTED && 43 ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == 44 PackageManager.PERMISSION_GRANTED 45 46 if (permissionAccessCoarseLocationApproved) { 47 val backgroundLocationPermissionApproved = ActivityCompat 48 .checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == 49 PackageManager.PERMISSION_GRANTED 50 51 if (backgroundLocationPermissionApproved) { 52 // フォアグラウンドとバックグランドのバーミッションがある 53 } else { 54 // フォアグラウンドのみOKなので、バックグラウンドの許可を求める 55 ActivityCompat.requestPermissions(this, 56 arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), 57 PERMISSION_REQUEST_CODE 58 ) 59 } 60 } else { 61 // 位置情報の権限が無いため、許可を求める 62 ActivityCompat.requestPermissions(this, 63 arrayOf( 64 Manifest.permission.ACCESS_COARSE_LOCATION, 65 Manifest.permission.ACCESS_FINE_LOCATION, 66 Manifest.permission.ACCESS_BACKGROUND_LOCATION 67 ), 68 PERMISSION_REQUEST_CODE 69 ) 70 } 71 } 72 73 private fun createNotificationChannel(){ 74 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 75 val channel = NotificationChannel( 76 LocationService.CHANNEL_ID, 77 "通知", 78 NotificationManager.IMPORTANCE_DEFAULT).apply { 79 description = "からのお知らせ" 80 } 81 val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 82 notificationManager.createNotificationChannel(channel) 83 } 84 } 85 86 87} 88

Kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.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 tools:context=".MainActivity"> 8 9 <Button 10 android:id="@+id/startButton" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Start" 14 app:layout_constraintBottom_toTopOf="@id/finishButton" 15 app:layout_constraintLeft_toLeftOf="parent" 16 app:layout_constraintRight_toRightOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18 19 <Button 20 android:id="@+id/finishButton" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="Finish" 24 app:layout_constraintBottom_toBottomOf="parent" 25 app:layout_constraintLeft_toLeftOf="parent" 26 app:layout_constraintRight_toRightOf="parent" 27 app:layout_constraintTop_toBottomOf="@id/startButton" /> 28 29 30</androidx.constraintlayout.widget.ConstraintLayout>

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

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

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

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

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

guest

回答1

0

StartButtonが参照されていないのが原因だと思います
setContentView(R.layout.activity_main)の下に

val startButton = findViewById<Button>(R.id.startButton)
val finishButton = findViewById<Button>(R.id.finishButton)

でひとまず参照先を作成してエラーが出ないことは確認できたので試してみてください

投稿2021/08/06 07:32

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問