Android 底部 sheet 更新支持库后崩溃

Android bottom sheet crash after update support library

我们有一个底部 sheet,在将支持库版本更新为“26.0.1”后,当您打开应用程序并在显示底部 sheet 之前,应用程序崩溃了。

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/vf_ovelay_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />

        <FrameLayout
            android:id="@+id/bottom_sheet_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="32dp"
            android:clickable="true"
            android:visibility="gone"
            tools:visibility="visible"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            app:behavior_skipCollapsed="true"/>

</android.support.design.widget.CoordinatorLayout>

private BottomSheetBehavior mBottomSheetBehavior;
private ViewGroup bottomSheet;

这是我的展示方式

mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setHideable(true);//Important to add
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheet.removeAllViews();
bottomSheet.setVisibility(VISIBLE);
bottomSheet.addView(getCustomView());

当我点击屏幕时,应用程序崩溃了!

日志是

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at android.support.design.widget.BottomSheetBehavior.onInterceptTouchEvent(BottomSheetBehavior.java:299)
at android.support.design.widget.CoordinatorLayout.performIntercept(CoordinatorLayout.java:460)
at android.support.design.widget.CoordinatorLayout.onInterceptTouchEvent(CoordinatorLayout.java:499)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2310)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2568)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1776)
at android.app.Activity.dispatchTouchEvent(Activity.java:2866)

这只能通过创建自定义 BottomSheetBehavior class 并更新 onInterceptTouchEvent 方法来解决。

更多信息: https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior

https://github.com/google/ground-android/blob/master/gnd/src/main/java/com/google/android/gnd/ui/common/BottomSheetBehavior.java

别忘了将它迁移到 AndroidX:https://developer.android.com/jetpack/androidx/migrate