草庐IT

android - java.lang.IllegalArgumentException : The view is not associated with BottomSheetBehavior 异常

coder 2023-12-18 原文

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

我创建了一个预期用作 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_width布局高度:

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

有关android - java.lang.IllegalArgumentException : The view is not associated with BottomSheetBehavior 异常的更多相关文章

随机推荐