我目前正在测试androidx.camera.view.CameraView
,但是当我将其与ConstraintLayout一起使用(也与FrameLayout一起使用)时,会引发渲染问题。
我正在使用的CameraX库的版本为:androidx.camera:camera-core:1.0.0-alpha06
androidx.camera:camera-camera2:1.0.0-alpha06
,androidx.camera:camera-view:1.0.0-alpha03
和androidx.camera:camera-extensions:1.0.0-alpha03
。
以下是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable name="recordedDetectionViewModel"
type="com.biolab.plr.screens.recorded.RecordedDetectionViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.recorded.RecordedDetectionFragment">
<TextView
android:id="@+id/fpsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/cameraView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:text="@{@string/fps(recordedDetectionViewModel.fps)}"
/>
<androidx.camera.view.CameraView
app:layout_constraintTop_toBottomOf="@+id/fpsTextView"
app:layout_constraintBottom_toTopOf="@+id/recordButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/cameraView"
android:layout_width="0dp"
android:layout_height="0dp"/>
<Button
android:id="@+id/recordButton"
android:text="@{recordedDetectionViewModel.buttonText}"
android:onClick="@{() -> recordedDetectionViewModel.onRecordButtonClicked()}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这是渲染错误后的堆栈跟踪:
java.lang.NullPointerException
at android.hardware.display.DisplayManager.registerDisplayListener(DisplayManager.java:414)
at androidx.camera.view.CameraView.onAttachedToWindow(CameraView.java:328)
at android.view.View.dispatchAttachedToWindow(View.java:18347)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3397)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:42)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:330)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:394)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:200)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:572)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:698)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)