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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BottomSheetBehavior 之 java.lang.IllegalArgumentException: The view is not associated with BottomShe相关的知识,希望对你有一定的参考价值。

09-14 22:52:58.891 13674-13674/me.chunsheng.uberdriver E/androidRuntime: FATAL EXCEPTION: main
                                                                         Process: me.chunsheng.uberdriver, PID: 13674
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{me.chunsheng.uberdriver/me.chunsheng.uberdriver.MainActivity}: java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2503)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2570)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:162)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1446)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:135)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5528)
                                                                             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:952)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
                                                                          Caused by: java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior
                                                                             at android.support.design.widget.BottomSheetBehavior.from(BottomSheetBehavior.java:710)
                                                                             at me.chunsheng.uberdriver.MainActivity.onCreate(MainActivity.java:27)
                                                                             at android.app.Activity.performCreate(Activity.java:6074)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2456)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2570) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:162) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1446) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:135) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5528) 
                                                                             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:952) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
09-14 22:52:58.951 13674-13674/me.chunsheng.uberdriver I/Process: Sending signal. PID: 13674 SIG: 9

 错误信息如上:view与BottomSheetBehavior不匹配???

 

这是什么鬼呢???

 

由于BottomSheetBehavior填充的view需要为 android.support.design.widget.CoordinatorLayout 的一个子view,这时候,比如view在布局中为一个相对布局,需要这样写:

 

 <RelativeLayout
        android:id="@+id/layout_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@android:color/white"
        android:elevation="8dp"
        app:behavior_hideable="true"
        app:behavior_peekHeight="90dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <TextView
            android:id="@+id/text_view_sheet_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/activity_vertical_margin"
            android:text="点击叫车,上滑选车"
            android:textSize="14sp" />

    </RelativeLayout>

 注意红色声明,这时候用BottomSheetBehavior填充这个view的时候才是正确的.

以上是关于BottomSheetBehavior 之 java.lang.IllegalArgumentException: The view is not associated with BottomShe的主要内容,如果未能解决你的问题,请参考以下文章

BottomSheetBehavior 不在 androidX 库中

使用 BottomSheetBehavior 显示片段

Android使用BottomSheetBehavior 和 BottomSheetDialog实现底部弹窗

Android使用BottomSheetBehavior 和 BottomSheetDialog实现底部弹窗

将 BottomSheetBehavior 与内部 CoordinatorLayout 一起使用

嵌套的 RecyclerView 滚动无法向下滚动 ViewPager2 的 BottomSheetBehavior