【Android Studio】kotlinにて公式ドキュメントに沿ってCameraXの実装をしたが動作しない

実現したいこと

kotlinにてandroid cameraXを実現したい

前提

開発環境 
Android Studio Giraffe | 2022.3.1 Patch 2
Build #AI-223.8836.35.2231.10811636, built on September 15, 2023
Runtime version: 17.0.6+0-b2043.56-10027231 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 16
Registry:
external.system.auto.import.disabled=true
debugger.new.tool.window.layout=true
ide.text.editor.with.preview.show.floating.toolbar=false
ide.experimental.ui=true

言語 kotlin
cameraX公式ドキュメント通りにコーディング中
公式ドキュメント➡https://developer.android.com/codelabs/camerax-getting-started?hl=ja

発生している問題・エラーメッセージ

kotlin

1override fun onRequestPermissionsResult(2 requestCode: Int, permissions: Array<String>, grantResults:3 IntArray) {4 if (requestCode == REQUEST_CODE_PERMISSIONS) {5 if (allPermissionsGranted()) {6 startCamera()7 } else {8 Toast.makeText(this,9 "Permissions not granted by the user.",10 Toast.LENGTH_SHORT).show()11 finish()12 }13 }14}15

上記コードのonRequestPermissionsResultメソッドのところで赤の下線が出る。エミュレータでの実行は通るがすぐにアプリが落ちてしまう

kotlin

1override fun onRequestPermissionsResult(2 requestCode: Int, permissions: Array<String>, grantResults:3 IntArray) {4 super.onRequestPermissionsResult(requestCode, permissions, grantResults)5 if (requestCode == REQUEST_CODE_PERMISSIONS) {6 if (allPermissionsGranted()) {7 startCamera()8 } else {9 Toast.makeText(this,10 "Permissions not granted by the user.",11 Toast.LENGTH_SHORT).show()12 finish()13 }14 }15 }

上記のようにエディタで指示されたように書き換えてもすぐにアプリが落ちてしまう。

試したこと

onRequestPermissionsResultの代わりにonActivityResultに変更して試しても起動することがなかった。

gradleの追加箇所

build.gradle.kts

1plugins { 2 id("com.android.application") 3 id("org.jetbrains.kotlin.android") 4} 5 6android { 7 namespace = "com.example.cameraxapp" 8 compileSdk = 33 9 10 defaultConfig { 11 applicationId = "com.example.cameraxapp" 12 minSdk = 30 13 targetSdk = 34 14 versionCode = 1 15 versionName = "1.0" 16 17 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 18 } 19 20 buildTypes { 21 release { 22 isMinifyEnabled = false 23 proguardFiles( 24 getDefaultProguardFile("proguard-android-optimize.txt"), 25 "proguard-rules.pro" 26 ) 27 } 28 } 29 compileOptions { 30 sourceCompatibility = JavaVersion.VERSION_1_8 31 targetCompatibility = JavaVersion.VERSION_1_8 32 } 33 kotlinOptions { 34 jvmTarget = "1.8" 35 } 36 buildFeatures { 37 viewBinding = true 38 } 39} 40 41dependencies { 42 //追加 43 // CameraX core library using the camera2 implementation 44 val camerax_version = "1.3.0-alpha04" 45 // The following line is optional, as the core library is included indirectly by camera-camera2 46 implementation("androidx.camera:camera-core:${camerax_version}") 47 implementation("androidx.camera:camera-camera2:${camerax_version}") 48 // If you want to additionally use the CameraX Lifecycle library 49 implementation("androidx.camera:camera-lifecycle:${camerax_version}") 50 // If you want to additionally use the CameraX VideoCapture library 51 implementation("androidx.camera:camera-video:${camerax_version}") 52 // If you want to additionally use the CameraX View class 53 implementation("androidx.camera:camera-view:${camerax_version}") 54 // If you want to additionally add CameraX ML Kit Vision Integration 55 implementation("androidx.camera:camera-mlkit-vision:${camerax_version}") 56 // If you want to additionally use the CameraX Extensions library 57 implementation("androidx.camera:camera-extensions:${camerax_version}") 58 59}

コメントを投稿

0 コメント