代码之家  ›  专栏  ›  技术社区  ›  Christian Serrano

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

  •  7
  • Christian Serrano  · 技术社区  · 8 年前

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

    我创建了一个布局,该布局预期将作为底页:

    <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 回复  |  直到 8 年前
        1
  •  32
  •   Shubham Suryavanshi Ramz    6 年前

    我已经解决了这个问题,我想发布答案,以防其他人也有同样的问题,当你导入包含底部页面的布局时,你不能包含 layout_width layout_height :

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

    <include layout="@layout/btmsht_principal"/>
    
        2
  •  2
  •   Guilherme Lemmi MjJ    6 年前

    底页主要内容用途:

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

    app:layout_behavior=“com.google.android.material.bottomsheet.BottomSheetBehavior”

    你的问题解决了

        3
  •  0
  •   Vinod Pattanshetti    8 年前

    <?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>
    
        4
  •  0
  •   Michael Osofsky    7 年前

    当我遇到这个问题时,上面的解决方案并没有解决它。我发现我必须复制 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>
    

    以下是布局/ll\u poi\u视图:

    <?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>
    
        5
  •  0
  •   Rxhack com    6 年前

    调用底部工作表的主要活动

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