maohieng maohieng - 25 days ago 15
Android Question

Upgrade Android Support library to 24.2.0 makes my app crash

My previous support library version is 24.1.1. The app works fine. But after upgrade to version 24.2.0, the app always force stop when start. My app's MainActivity contains a

BottomSheetBehavior
view and
FloatingActionButton
. Here's the error message:

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)
...
...


Update: Here is my MainActivity's layout

<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>

Answer

I don't know if it is a bug, but currently with the 24.2.0 the FAB can not longer be anchored to indirect children of CoordinatorLayout.

The layout_anchor only works with direct children of CoordinatorLayout. You'll need to change your layout_anchor to use a different view.

You can check the source code of the 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;
        }

More details here.

Comments