[英]While trying to implement CameraX, the app crashes
我試圖制作一個簡單的 CameraX 應用程序。 我正在為我的應用上傳 github 鏈接: https://github.com/srivastavapoorv/GithubPracticeAndroid/tree/master/CameraX
當我嘗試運行它時,應用程序崩潰並且AndroidStudio在logcat中給出了這個錯誤: https://i2.paste.pics/c77714ec26373276884c6d490f3bbe40.png
這是 AndroidManifest.xml `
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraX">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
`
構建.gradle(應用程序)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android { compileSdkVersion 30 buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.camerax"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
依賴{
def camerax_version = "1.0.0-alpha06"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//noinspection GradleDependency
implementation "androidx.camera:camera-core:$camerax_version"
//noinspection GradleDependency
implementation "androidx.camera:camera-camera2:$camerax_version"
}
請幫我解決這個問題。
這是您遇到崩潰的代碼
preview.setOnPreviewOutputUpdateListener {
val parent = textureView.parent as ViewGroup
parent.removeView(textureView)
// crash on next line, "cannot add a null child view to a ViewGroup"
parent.addView(textureView, 0)
textureView.setSurfaceTexture(it.surfaceTexture)
}
所以說textureView
是null,但是你只是調用textureView.parent
和removeView(textureView)
而沒有崩潰,所以在removeView
調用之后它一定是null。
您正在使用合成導入( import kotlinx.android.synthetic.main.activity_main.*
行),它正在從您的布局文件創建textureView
引用 - 我不確切知道它如何處理緩存的東西,但猜測,調用removeView
(從布局中刪除視圖)會導致合成內容丟失/丟棄其對該視圖 object 的引用。 所以值變成了 null,而你已經失去了它。
Synthetics 已被棄用,但您可以通過獲取您自己對該視圖的顯式引用來解決此問題。 您可以使用findViewById
或僅從合成變量中獲取引用:
// top-level variable - you need to grab and keep a reference before it gets lost
val preciousTextureView: TextureView = findViewById(R.id.textureView)
// or
val preciousTextureView: TextureView = textureView
第一個肯定會工作,第二個應該。 然后你只需在代碼中使用preciousTextureView
而不是textureView
java.lang.IllegalArgumentException:從 hal 返回的支持的圖片大小列表是 null。 at com.android.camera.PictureSizeManager.initialize(PictureSizeManager:java.111) at com.android.camera.camera_adapter.CameraQcom.getBestPictureSize(CameraQcom:java.432) at com.android.camera.module.CameraModule.updateCameraParametersPreference(CameraModule :java.3971) at com.android.camera.camera_adapter.CameraQcom.updateCameraParametersPreference(CameraQcom:java.376) at com.android.camera.module.CameraModule.setCameraParameters(CameraModule:java.4342) at com.android.camera. module.CameraModule$Came raStartUpThread.run(CameraModule:java:415)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.