首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在尝试实现CameraX时,应用程序崩溃了

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

Stack Overflow用户
提问于 2021-05-13 20:49:41
回答 1查看 584关注 0票数 0

我试着制作一个简单的CameraX应用程序。我正在为我的应用程序上传github链接:https://github.com/srivastavapoorv/GithubPracticeAndroid/tree/master/CameraX

当我尝试运行它时,应用程序崩溃了,AndroidStudio在logcat:https://i2.paste.pics/c77714ec26373276884c6d490f3bbe40.png中给出了这个错误

这是AndroidManifest.xml‘

代码语言:javascript
复制
<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)

代码语言:javascript
复制
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'

}

安卓{ compileSdkVersion 30 buildToolsVersion "30.0.3“

代码语言:javascript
复制
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'
}

}

依赖关系{

代码语言:javascript
复制
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"

}

请帮我解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-14 01:12:26

这是你要崩溃的代码

代码语言:javascript
复制
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调用之后,它一定变成了null。

您使用的是合成导入( import kotlinx.android.synthetic.main.activity_main.*行),它从布局文件中创建textureView引用--我不知道它到底是如何处理缓存的,但据猜测,调用removeView (从布局中移除视图)会导致合成内容丢失/删除对该视图对象的引用。因此,该值变为空,并且您已经丢失了它。

合成技术是不受欢迎的,但是你可以通过获取你自己对这个视图的明确引用来解决这个问题。您可以使用findViewById或者从合成变量中获取一个引用:

代码语言:javascript
复制
// 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67526081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档