我正在尝试在干净的android应用程序中包含一个CameraView组件(来自CameraX jetpack),如下所示:

https://medium.com/@hitherejoe/exploring-camerax-on-android-camera-view-daae6dfaa4ec
https://developer.android.google.cn/jetpack/androidx/releases/camera

CameraView非常简单,但是将其绑定到活动生命周期时出了点问题。活动已授予摄影机许可。我曾尝试在没有CameraView的情况下使用CameraX(具有SurfaceView +所有设置),但最终结果和异常相同(CameraView的使用更加简单)。在Samsung S10和Redmi Note 7上进行了测试。似乎组件尺寸/测量存在问题:

例外

java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
        at androidx.camera.core.Preview.updateListener(Preview.java:368)
        at androidx.camera.core.Preview.updateOutput(Preview.java:586)
        at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363)
        at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524)
        at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408)
        at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684)
        at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195)
        at androidx.camera.view.CameraXModule.bindToLifecycleAfterViewMeasured(CameraXModule.java:268)
        at androidx.camera.view.CameraView.onMeasure(CameraView.java:364)
        at android.view.View.measure(View.java:24973)
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:992)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3289)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2016)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2318)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1876)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8499)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.ClassCastException: Bootstrap method returned null
        at androidx.camera.core.Preview.updateListener(Preview.java:368) 
        at androidx.camera.core.Preview.updateOutput(Preview.java:586) 
        at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363) 
        at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524) 
        at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408) 
        at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684) 
        at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195) 
        at androidx.camera.view.CameraXModule.bindToLifecycleAfterViewMeasured(CameraXModule.java:268) 
        at androidx.camera.view.CameraView.onMeasure(CameraView.java:364) 
        at android.view.View.measure(View.java:24973) 
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227) 
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) 
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825) 
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:992) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3289) 
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2016) 
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2318) 
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1876) 
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8499) 
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) 
        at android.view.Choreographer.doCallbacks(Choreographer.java:761) 
        at android.view.Choreographer.doFrame(Choreographer.java:696) 
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7037) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 


依存关系

implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    def camerax_version = "1.0.0-alpha06"
    def camerax_view_version = "1.0.0-alpha03"
    def camerax_ext_version = "1.0.0-alpha03"

    implementation "androidx.camera:camera-core:$camerax_version"
    implementation "androidx.camera:camera-camera2:$camerax_version"
    implementation "androidx.camera:camera-view:$camerax_view_version"
    implementation "androidx.camera:camera-extensions:$camerax_ext_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.camera.view.CameraView
        android:id="@+id/view_camera"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


主要

class MainActivity : AppCompatActivity(), LifecycleOwner {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        view_camera.bindToLifecycle(this)
    }
}


我感觉好像缺少了一些简单的东西,但是在这方面我是很新的,而且由于CameraX很新,因此对此没有太多疑问。

最佳答案

解决的问题:我必须启用Java 8兼容性:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}

10-02 02:09