1

Getting error while setting BottomSheetBehavior in bottom sheet

Main Linear layout of bottom sheet :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="444dp"
    android:background="#F0F0F1"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:padding="10dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="120dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

Code where i set bottom sheet :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".RatingsActivity">

    <include
        android:id="@+id/contentMain"
        layout="@layout/content_main" />

    <include
        android:id="@+id/bottomSheet"
        layout="@layout/bottom_sheet" />
</RelativeLayout>

This is how i set the bottomsheet behavior callback in code :

 bottom_sheet = findViewById(R.id.bottom_sheet);
 sheetBehavior = BottomSheetBehavior.from(bottom_sheet);

 sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int i) {

                if (i == BottomSheetBehavior.STATE_DRAGGING) {
                    sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            }

            @Override
            public void onSlide(@NonNull View view, float v) {

            }
        });

But it shows error :

"Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference"

in :

sheetBehavior = BottomSheetBehavior.from(bottom_sheet);
Karan Mehta
  • 1,442
  • 13
  • 32
  • 1
    You've set an `android:id` on the `` tag, and that overrides the `android:id` on the root `View` in the included layout. Your `LinearLayout` is ending up with ID `bottomSheet`, instead of `bottom_sheet`, so either change the `findViewById()` call to pass `R.id.bottomSheet`, or remove/change the `android:id` on the ``. – Mike M. Dec 19 '19 at 05:46
  • Now getting error : "The view is not a child of CoordinatorLayout" – Karan Mehta Dec 19 '19 at 05:52
  • Oh, yeah, I didn't even notice that. `BottomSheetBehavior` only works in a `CoordinatorLayout`. You have a `RelativeLayout`. `Behavior`s are strictly a `CoordinatorLayout` thing. They don't work with other `ViewGroup`s. If you don't want to switch to a `CoordinatorLayout`, then possibly a `BottomSheetDialog`/`BottomSheetDialogFragment` would work for you. Those don't require a `CoordinatorLayout` in the `Activity`; they provide their own. – Mike M. Dec 19 '19 at 05:54
  • 1
    It solved my problem, Thank you so much – Karan Mehta Dec 19 '19 at 06:01

0 Answers0