将 Android 支持库升级到 24.2.0 使我的应用程序崩溃

Upgrade Android Support library to 24.2.0 makes my app crash

我之前的支持库版本是24.1.1。该应用程序运行良好。但是升级到24.2.0版本后,应用程序总是在启动时强制停止。我的应用程序的 MainActivity 包含一个 BottomSheetBehavior 视图和 FloatingActionButton。这是错误消息:

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams
at android.support.design.widget.FloatingActionButton$Behavior.isBottomSheet(FloatingActionButton.java:597)
at android.support.design.widget.FloatingActionButton$Behavior.onDependentViewChanged(FloatingActionButton.java:589)
at android.support.design.widget.FloatingActionButton$Behavior.onDependentViewChanged(FloatingActionButton.java:528)
at android.support.design.widget.CoordinatorLayout.offsetChildToAnchor(CoordinatorLayout.java:1564)
at android.support.design.widget.CoordinatorLayout.onChildViewsChanged(CoordinatorLayout.java:1233)
at android.support.design.widget.CoordinatorLayout$OnPreDrawListener.onPreDraw(CoordinatorLayout.java:1812)
...
...

更新:这是我的 MainActivity 的布局

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.MainActivity">

    <include
        android:id="@id/appBar"
        layout="@layout/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:viewPagerTab="@{(currentFragment instanceof SongBookTabFragment)? viewPagerTab : null}" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:onClick="clickFAB"
        app:layout_anchor="@id/container"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="com.boombile.originalsong.ui.widget.ScrollAwareFABBehavior"
        app:srcCompat="@drawable/ic_menu_send" />

    <include
        android:id="@+id/bottomSheetPlayer"
        layout="@layout/view_bottom_sheet_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/bottom_sheet_behavior" />

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

我不知道这是否是一个错误,但目前使用 24.2.0,FAB 不能再锚定到 CoordinatorLayout 的间接子级。

layout_anchorCoordinatorLayout 的直接子代 一起工作。您需要更改 layout_anchor 以使用不同的视图。

您可以查看 FloatingActionButton:

的源代码
        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,
                View dependency) {
            if (dependency instanceof AppBarLayout) {
                // If we're depending on an AppBarLayout we will show/hide it automatically
                // if the FAB is anchored to the AppBarLayout
                updateFabVisibilityForAppBarLayout(parent, (AppBarLayout) dependency, child);
            } else if (isBottomSheet(dependency)) {
                updateFabVisibilityForBottomSheet(dependency, child);
            }
            return false;
        }

        private static boolean isBottomSheet(View view) {
            CoordinatorLayout.LayoutParams lp =
                    (CoordinatorLayout.LayoutParams) view.getLayoutParams();
            return lp != null && lp.getBehavior() instanceof BottomSheetBehavior;
        }

更多 details here.

这是一个已知错误,请参阅 https://code.google.com/p/android/issues/detail?id=220250

你应该升级到24.2.1版本,这个问题已经解决了。