当我尝试获取 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>
谁能帮助我,我在哪里做错了?