将Android支持库升级到24.2.0会导致我的应用崩溃

时间:2016-09-01 03:56:24

标签: android android-support-library floating-action-button android-support-design

我以前的支持库版本是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>

2 个答案:

答案 0 :(得分:3)

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

layout_anchor 适用于CoordinatorLayout的直接子女。您需要更改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

答案 1 :(得分:0)

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

你应该升级到24.2.1版本,这个问题在那里解决了。