Я хочу реализовать компонент BottomSheetBehavoir на экране активности, но когда я хочу инициализировать компонент с помощью BottomSheetBehavoir.from(view)
, метод getLayoutParams () вызывает исключение нулевого указателя.
Это мой код инициализации в onCreate ():
LinearLayout llBottomSheet = findViewById(R.id.bottom_sheet_live_tracking_list_container);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(llBottomSheet);
И это мой xml макет:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet_live_tracking_list_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:behavior_hideable="false"
app:behavior_peekHeight="40dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@drawable/rounded_corners_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/show_more"
android:textColor="@color/white"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/activity_live_tracking_show_more_tv" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/ic_filter_list"
android:visibility="visible" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/activity_live_tracking_position_list_rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Я включаю этот макет в свой root xml макет с помощью тега include.
Код ошибки:
2020-02-21 12:34:05.097 8419-8419/de.app.haveltec.ilockit E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.app.haveltec.ilockit, PID: 8419
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.app.haveltec.ilockit/de.app.haveltec.ilockit.screens.livetracking.LiveTrackingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at com.google.android.material.bottomsheet.BottomSheetBehavior.from(BottomSheetBehavior.java:981)
at de.app.haveltec.ilockit.screens.livetracking.LiveTrackingActivity.onCreate(LiveTrackingActivity.java:147)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)