gpt4 book ai didi

android - 将 CameraX + CameraView 绑定(bind)到生命周期时出现问题

转载 作者:行者123 更新时间:2023-12-02 10:34:37 24 4
gpt4 key购买 nike

我正在尝试在干净的 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 来说非常简单,但是将其绑定(bind)到 Activity 生命周期时出现了问题。 Activity 已授予相机权限。我尝试过在没有 CameraView 的情况下使用 CameraX(使用 SurfaceView + 所有设置),但以相同的结果和异常结束(并且 CameraView 使用起来更简单)。在三星 S10 和红米 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"
}

关于android - 将 CameraX + CameraView 绑定(bind)到生命周期时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com