Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know this have been asked before but even though I checked the answers I don't found any solution to my specific problem:

I create a layout that is expected to act as a bottomsheet:

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

I use it in the coordinator layout:

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

But whe I try to get the reference with:

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

I get the error:

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)

Is not duplicated because mine is not nested and is a direct child of Coordinator Layout

like image 230
Christian Serrano Avatar asked Jul 27 '17 16:07

Christian Serrano


1 Answers

I've solved it, and I want to post the answer in case someone else has the same problem, when you import the layout that contains the BottomSheet you must not include layout_width and layout_height:

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

so it has to be:

<include layout="@layout/btmsht_principal"/>
like image 112
Christian Serrano Avatar answered Sep 27 '22 22:09

Christian Serrano