Java LinearLayout中的getLayoutParams()返回null
我想在活动屏幕上实现BottomSheetBehavoir组件,但是当我想通过Java LinearLayout中的getLayoutParams()返回null,java,android,android-linearlayout,bottom-sheet,Java,Android,Android Linearlayout,Bottom Sheet,我想在活动屏幕上实现BottomSheetBehavoir组件,但是当我想通过BottomSheetBehavoir.from(view)初始化组件时,getLayoutParams()方法会抛出空指针异常 这是我在onCreate()中的初始化代码: 这是我的xml布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.co
BottomSheetBehavoir.from(view)
初始化组件时,getLayoutParams()方法会抛出空指针异常
这是我在onCreate()中的初始化代码:
这是我的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" />
如下图所述,行为已被弃用。如果仍要使用它,请确保它是Coordinator布局的子项,因为根据文档,这是使用它的先决条件。
我宁愿添加这个答案作为评论,但声誉不够…因此,在我休息几天后,回到问题上,我找到了解决方案。对于引发getLayoutParams()空指针异常的LinearLayout,我使用了底部工作表xml中的id,您可以在我的帖子
android:id=“@+id/bottom\u sheet\u live\u tracking\u list\u container”
中看到该id。在我的主布局中,父布局是一个坐标布局,包括我的底部图纸布局,如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
... other stuff ....
<include
android:id="@+id/activity_live_tracking_bottom_sheet"
layout="@layout/bottom_sheet_live_tracking_list" />
... 其他东西。。。。
知道吗,我尝试了include标签中的id(
android:id=“@+id/activity\u live\u tracking\u bottom\u sheet”
)并且成功了。我无法解释为什么标记LinearLayout的另一个id直接为getLayoutParams()方法返回null。您可以添加错误日志吗?您的底部工作表\u live\u tracking\u列表\u容器
是CoordinatorLayout
的子容器吗?是的,当我运行应用程序而不在我的活动中初始化它时,底部工作表工作正常。您使用的是哪个版本的com.android.support:design
?我使用android x迁移,还是在使用android x时我还必须设置设计库?底部工作表行为在AndroidStudio中没有标记为不推荐,它是行为主义者的协调布局法。因此,请使用addBottomSheetCallback(BottomSheetCallback)代替我的应用程序崩溃,因为LinearLayout子项中的getLayoutParams()为null,这与回调侦听器无关
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)
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
... other stuff ....
<include
android:id="@+id/activity_live_tracking_bottom_sheet"
layout="@layout/bottom_sheet_live_tracking_list" />