java.lang.IllegalArgumentException: 该视图未与 BottomSheetBehavior 关联

java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior

我知道之前有人问过这个问题,但即使我检查了答案,我也没有找到解决我的具体问题的方法:

我创建了一个预期用作底页的布局:

<android.support.constraint.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:id="@+id/btmsht"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:orientation="vertical"
android:layout_height= "300dp"
android:layout_width="match_parent"
tools:showIn="@layout/activity_principal">

我在协调器布局中使用它:

<include layout="@layout/btmsht_principal" android:layout_width="match_parent"
    android:layout_height="match_parent"/>

但是当我尝试通过以下方式获取参考时:

View tview = findViewById(R.id.btmsht);
    btmsht = BottomSheetBehavior.from(tview);

我收到错误:

java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access0(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior
        at android.support.design.widget.BottomSheetBehavior.from(BottomSheetBehavior.java:816)
        at com.blixter.fiesta.Principal.creacionViews(Principal.java:69)
        at com.blixter.fiesta.Principal.onCreate(Principal.java:57)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access0(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method)

没有重复,因为我的没有嵌套并且是 Coordinator Layout 的直接子项

我已经解决了,我想 post 答案以防其他人遇到同样的问题,当您导入包含 BottomSheet 的布局时,您不能包含 layout_widthlayout_height:

<include layout="@layout/btmsht_principal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

所以它必须是:

<include layout="@layout/btmsht_principal"/>

CoordinatorLayout 总是需要一个直接关联的子项,然后只有它才能工作,否则它将通过错误 "The view is not associated with BottomSheetBehavior"。在下面的 .xml 文件中,第二个相对布局是 CoordinatorLayout 的直接子级,即底部 sheet。在标签中我们可以添加 android:layout_width 和 android:layout_height.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/rooms_background_txt"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include
            android:id="@+id/toolbar_listing"
            layout="@layout/actionbar_room_selection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_white"
        android:gravity="center"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

当我遇到这个问题时,上面的解决方案都没有解决。我发现我必须在<include>标签上复制app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior",如下:

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

    <include
        android:id="@+id/ll_poi_view"
        layout="@layout/ll_poi_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这里是layout/ll_poi_view:

<?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="wrap_content"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <TextView
        android:id="@+id/headerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="This area is for header content"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/poi_content_scrollview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/poi_content_scrollview"
        android:layout_width="395dp"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/headerTextView">

        <include
            android:id="@+id/poi_content"
            layout="@layout/ll_poi_view_scrollable_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

底部sheet主要内容使用:

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

在 Main activity 中与 include 布局一起使用 app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"

您的问题已解决

Main activity to call the bottom sheet

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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=".INT.InternalAssessment"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView13"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/internal_marks"
                android:textSize="24sp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <include layout="@layout/recycle_view_internal_assessment_cards" />

        </LinearLayout>

        <include
            layout="@layout/bottom_sheet_detail_view_internal_assessment"
            android:layout_width="match_parent"
            android:layout_height="340dp"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>

        <!--<android.support.v7.widget.RecyclerView-->
            <!--android:id="@+id/sem_recycle"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="match_parent" />-->

    </android.support.design.widget.CoordinatorLayout>

Bottom sheet with conetnt

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="340dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center_horizontal|center"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="80dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:cardCornerRadius="10dp">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/colorPrimary">

                    <TextView
                        android:id="@+id/textView11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="@string/subject" />

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="@string/_1st_semister" />

                    <TextView
                        android:id="@+id/textView9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="@string/_2nd_semister" />
                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView15"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />

                    <TextView
                        android:id="@+id/textView14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />
                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView22"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />

                    <TextView
                        android:id="@+id/textView16"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />

                    <TextView
                        android:id="@+id/textView5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />
                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="63dp">

                    <TextView
                        android:id="@+id/textView24"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />

                    <TextView
                        android:id="@+id/textView23"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:padding="5dp"
                        android:text="TextView" />
                </TableRow>
            </TableLayout>
        </android.support.v7.widget.CardView>

    </LinearLayout>