[英]Is there a problem is using a RecyclerView as a child of MotionLayout?
因此,我正在嘗試創建一個由片段容器和其他兩個視圖組成的動態場景,以使用滑動手勢進行播放。 問題是,只要片段容器包含回收器視圖,並且存在 OnSwipe 轉換,應用程序就會崩潰並出現 null 指針異常。 這是我的代碼
這是包含 MotionLayout 的主布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_single_scene03"
tools:context="com.mondiamedia.musicshop.activities.SingleActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true" />
<include layout="@layout/layout_mini_player" />
<include layout="@layout/layout_full_player" />
</androidx.constraintlayout.motion.widget.MotionLayout>
這是場景文件
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/startSet">
<Constraint
android:id="@+id/miniPlayerContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.9">
<PropertySet android:alpha="1" />
</Constraint>
<Constraint
android:id="@+id/fullPlayerContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="parent">
<PropertySet android:alpha="0" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/endSet">
<Constraint
android:id="@+id/miniPlayerContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.9">
<PropertySet android:alpha="0" />
</Constraint>
<Constraint
android:id="@+id/fullPlayerContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PropertySet android:alpha="1" />
</Constraint>
</ConstraintSet>
<Transition
app:constraintSetEnd="@id/endSet"
app:constraintSetStart="@id/startSet"
app:duration="1000">
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@id/miniPlayerContainer"
app:touchAnchorSide="top"
app:touchRegionId="@id/miniPlayerContainer" />
</Transition>
<Transition
app:constraintSetEnd="@id/startSet"
app:constraintSetStart="@id/endSet"
app:duration="1000">
<OnSwipe
app:dragDirection="dragDown"
app:touchAnchorId="@id/fullPlayerContainer"
app:touchAnchorSide="top"
app:touchRegionId="@id/fullPlayerContainer" />
</Transition>
</MotionScene>
每當我在 navHost 片段中有一個 RecyclerView 並嘗試滾動時,我都會被以下內容擊中
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference at androidx.constraintlayout.motion.widget.MotionLayout.onNestedPreScroll(MotionLayout.java:2200) at androidx. core.view.ViewParentCompat.onNestedPreScroll(ViewParentCompat.java:386) at androidx.core.view.NestedScrollingChildHelper.dispatchNestedPreScroll(NestedScrollingChildHelper.java:322) at androidx.recyclerview.widget.RecyclerView.dispatchNestedPreScroll(RecyclerView.java:11326) at androidx .recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5061) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927) at ZC31B32 364CE19CA8FCD150A417ECCE58Z.view.Choreographer.doCallbacks(Choreographer.java:702) at android.view.Choreographer.doFrame(Choreographer.java:635) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913) at android.os .Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main( ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAnd ArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
任何想法該怎么做以及為什么會這樣?
似乎是 ConstraintLayout 版本 2.0.0-beta7 的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.