我知道这已经被问过了,但是即使我检查了答案,也没有找到解决我特定问题的方法:

我创建了一个预期可以用作 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)

不重复,因为我的未嵌套并且是Coordinator Layout的直接子代

最佳答案

我已经解决了,如果其他人也有同样的问题,我想发布答案,当您导入包含BottomSheet的布局时,您不得包含layout_widthlayout_height:

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

所以它必须是:
<include layout="@layout/btmsht_principal"/>

关于android - java.lang.IllegalArgumentException : The view is not associated with BottomSheetBehavior,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45356704/

10-12 12:29