Android 在尝试实现CameraX时,应用程序崩溃

Android 在尝试实现CameraX时,应用程序崩溃,android,kotlin,android-camera2,android-camerax,Android,Kotlin,Android Camera2,Android Camerax,我试图制作一个简单的CameraX应用程序。我正在上载我的应用程序的github链接: 当我尝试运行它时,应用程序崩溃了 AndroidStudio在logcat中给出了这个错误: 这是AndroidManifest.xml ` } 安卓{ 编译DK30版 buildToolsVersion“30.0.3” } 依赖关系{ def camerax_version = "1.0.0-alpha06" implementation "org.jetbrains.

我试图制作一个简单的CameraX应用程序。我正在上载我的应用程序的github链接:

当我尝试运行它时,应用程序崩溃了 AndroidStudio在logcat中给出了这个错误:

这是AndroidManifest.xml `

}

安卓{ 编译DK30版 buildToolsVersion“30.0.3”

}

依赖关系{

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
为空,但您刚刚调用了
textureView.parent
removeView(textureView)
,没有崩溃,因此在调用
removeView
后它一定变为空

您正在使用合成导入(import kotlinx.android.synthetic.main.activity_main.*行)从布局文件中创建
textureView
引用-我不知道它是如何处理缓存的,但猜测一下,调用
removeView
(这会从布局中删除视图)导致合成材质丢失/删除对该视图对象的引用。因此该值变为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
在com.android.camera.module.CameraModule$CameraStartUpThread.run(CameraModule.java:415)

请在setContentView(R.layout.activity_main)之后添加此textureview=findViewById(R.id.textureview)作为textureview@Hascher谢谢,它工作编辑成功了…谢谢
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)
}
// 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