我一般对Kotlin和Android都很陌生。我试图做一个相机应用程序。当我启动该应用程序时,由于TextureView由于某种原因为null,它立即崩溃。我如何初始化它?很抱歉,如果它很琐碎,但我真的很难找到与此主题相关的最新教程和基于Kotlin的教程。更新:我没有在这里用方括号括起来,而是在代码中包含了方括号,不是由它引起的问题。
XML:
<TextureView
android:id="@+id/textureView"
android:layout_width="447dp"
android:layout_height="409dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/TakePicBtn"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
活动:
class CameraActivity : AppCompatActivity() {
companion object {
const val REQUEST_CAMERA_PERMISSION = 100
private val TAG = this::class.qualifiedName
}
private val cameraManager by lazy { getSystemService(Context.CAMERA_SERVICE) as CameraManager }
private fun <T> cameraCharacteristics(cameraId: String, key: CameraCharacteristics.Key<T>): T {
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
return when (key) {
CameraCharacteristics.LENS_FACING -> characteristics.get(key)
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP -> characteristics.get(key)
else -> throw IllegalArgumentException("Key not recognised")
}!!
}
private fun cameraId(lens: Int): String {
var deviceId = listOf<String>()
try {
val cameraIdList = cameraManager.cameraIdList
deviceId = cameraIdList.filter {
lens == cameraCharacteristics(
it,
CameraCharacteristics.LENS_FACING
)
}
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString())
}
return deviceId[0]
}
private fun connectCamera() {
val deviceId = cameraId(CameraCharacteristics.LENS_FACING_BACK)
Log.d(TAG, "devicesId: $deviceId")
}
override fun onResume() {
super.onResume()
if (textureView.isAvailable)
openCamera()
else{
textureView.setBackgroundColor(Color.GRAY)
textureView.surfaceTextureListener = surfaceListener
}
}
private val surfaceListener = object: TextureView.SurfaceTextureListener {
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
}
override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) = Unit
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?) = true
override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
Log.d(TAG, "width: $width height: $height")
openCamera()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults)
}
@AfterPermissionGranted(REQUEST_CAMERA_PERMISSION)
private fun checkCameraPermission() {
if (EasyPermissions.hasPermissions(this,CAMERA)){
Log.d(TAG,"APP HAS CAMERA PERMISSION")
connectCamera()
} else {
EasyPermissions.requestPermissions(this,"Camera app requires camera access",
REQUEST_CAMERA_PERMISSION, CAMERA)
}
}
private fun openCamera() {
checkCameraPermission()
// todo
}
}
输出:
java.lang.RuntimeException: Unable to resume activity {.../...CameraActivity}: java.lang.IllegalStateException: textureView must not be null
跟进:仍然不确定我的代码出了什么问题,但是即使没有TextureView,它也仍然坏了,我必须弄乱了活动。重新进行该活动,它开始工作。在onCreate中缺少setContentView(R.layout.activity_camera),但即使这样也没有帮助。祝所有遇到类似问题的人都好运。