java.lang.IllegalArgumentException:视图与BottomSheetBehavior无关

时间:2017-07-27 16:50:52

标签: android bottom-sheet

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

我创建了一个预期充当底层的布局:

<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.access$800(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.access$800(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)

不重复,因为我没有嵌套,并且是协调员布局的直接子项

5 个答案:

答案 0 :(得分:16)

我解决了它,我想发布答案以防其他人遇到同样的问题,当你导入包含底片的布局时,你必须不包括layout_width和layout_height:

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

所以它必须是:

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

答案 1 :(得分:0)

CoordinatorLayout总是需要一个直接关联的子项,然后只有它才会起作用,否则它将通过错误“视图与BottomSheetBehavior无关”。在下面的.xml文件中,第二个相对布局是CoordinatorLayout的直接子节点,这是底部工作表。在标签中我们可以添加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>

答案 2 :(得分:0)

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

<?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>

答案 3 :(得分:0)

在底页的主要内容中使用:

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

在主要活动中使用包含布局     app:layout_behavior =“ com.google.android.material.bottomsheet.BottomSheetBehavior”

您的问题已解决

答案 4 :(得分:0)

  

调用底页的主要活动

<?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>
  

带有竞争性的底页

<?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>