2017-07-27 218 views
0

相关的我知道这已经被问过,但即使我检查了答案,我也没有发现任何解决我的具体的问题:java.lang.IllegalArgumentException异常:视图不与BottomSheetBehavior

我创建了一个布局这有望充当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"> 

我用它在协调布局:

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

不重复,因为我的不是嵌套,是

+0

不,有答案是嵌套的,是不是嵌套在这里,是不是相同的问题 –

+1

oops ...你是对的。抱歉。 – BakaWaii

回答

4

我解决了这个问题协调员布局的直接孩子,我要发布的答案如果其他人有相同的问题,当您导入包含底部表格的布局哟不得包含layout_width和layout_height:

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

所以它必须是:

<include layout="@layout/btmsht_principal"/> 
+0

任何原因为什么不添加android:layout_height和android:layout_width在标记? –

+0

我想,你可以使用宽度和高度,因为它在文档中说:https://developer.android.com/training/improving-layouts/reusing-layouts.html,但你必须覆盖所有的布局参数,我没有测试过,但值得尝试,当你不重写所有内容时,它就会失败。 –

+0

好的......我从问题的理解是... ConstraintLayout需要直接关联的孩子,然后它会工作,否则它会给上述错误。 –

0

的CoordinatorLayout总是需要一个直接关联的子然后,然后只将工作,否则它会通过错误“的观点不符合BottomSheetBehavior关联”。在下面的.xml文件中,第二个相对布局是CoordinatorLayout的直接子节点,这是底部表单。在标签中,我们可以添加android:layout_width和android:layout_height。

<?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> 
相关问题