【问题标题】:While trying to implement CameraX, the app crashes尝试实现 CameraX 时,应用程序崩溃
【发布时间】:2021-05-13 20:49:41
【问题描述】:

我正在尝试制作一个简单的 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>

`

build.gradle(app)

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'

}

机器人{ 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"

}

请帮我解决这个问题。

【问题讨论】:

  • 请在 setContentView(R.layout.activity_main) 之后将此 textureview = findViewById(R.id.textureview) 添加为 TextureView
  • @Hascher 谢谢,成功了

标签: android kotlin android-camera2 android-camerax


【解决方案1】:

这是您遇到崩溃的代码

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.parentremoveView(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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2016-09-28
    • 2016-11-27
    相关资源
    最近更新 更多