尝试使用数据绑定获取 BottomSheetBehaviour 时出现 NullPointerException

NullPointerException when trying to get BottomSheetBehaviour, using Databinding

当我尝试获取 BottomSheetBehaviour 时出现 NullPointerException,下面是我的代码

MainActivity.kt

class MainActivity : AppCompatActivity() {


    lateinit var activityMainBinding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        val bottomSheet = activityMainBinding.bottomSheet.bottom_sheet_lienar_layout

        val bottomSheetBehavior =  BottomSheetBehavior.from(bottomSheet)<---NULL POINTER EXCEPTION

我的activity_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <RelativeLayout
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

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

我的bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet_lienar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is persistent Bottom Sheet"
        android:textSize="20sp" />

</LinearLayout>

谁能帮帮我,我哪里做错了?

在@Mike 和@Varun 的帮助下,我的代码按照 activity_main.xml 文件

中的更改工作
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="mainActivity"
            type="demo.section.bottomsheetex.MainActivity" />
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/bottomSheet"
            layout="@layout/bottom_sheet" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

和bottom_sheet.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:behavior_hideable="true"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is persistent Bottom Sheet"
        android:textSize="20sp" />
</LinearLayout>