设置BottomSheetBehavior时获取空指针异常

时间:2019-12-19 05:41:11

标签: android android-layout bottom-sheet

在底部工作表中设置BottomSheetBehavior时出错

底部工作表的主要线性布局:

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

我设置底页的代码:

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

这是我在代码中设置底层行为回调的方式:

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

            }
        });

但是显示错误:

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

在:

sheetBehavior = BottomSheetBehavior.from(bottom_sheet);

0 个答案:

没有答案