Search code examples
androidkotlinandroid-camera2android-camerax

While trying to implement CameraX, the app crashes


I was trying to make a simple CameraX app. I'm uploading the github link for my app: https://github.com/srivastavapoorv/GithubPracticeAndroid/tree/master/CameraX

When I try to run it, the app crashes And AndroidStudio gives this error in logcat: https://i2.paste.pics/c77714ec26373276884c6d490f3bbe40.png

This is 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'

}

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'
}

}

dependencies {

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"

}

Please help me resolve this.


Solution

  • Here's the code where you're getting the crash

    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)
    }
    

    so it's saying that textureView is null, but you just called textureView.parent and removeView(textureView) with no crashes, so it must have become null after the removeView call.

    You're using synthetic imports (the import kotlinx.android.synthetic.main.activity_main.* line) which is creating that textureView reference from your layout file - I don't know exactly how it handles caching things, but at a guess, calling removeView (which removes the view from the layout) causes the synthetic stuff to lose/drop its reference to that view object. So the value becomes null, and you've lost it.

    Synthetics is deprecated, but you can probably fix this by just grabbing your own explicit reference to that view. You could use findViewById or just grab a reference from the synthetic variable:

    // 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
    

    the first one will definitely work, the second one should. Then you just use preciousTextureView instead of textureView in your code