我以前的支持库版本是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>
我不知道这是否是一个bug,但目前与24.2.0 FAB不能再锚定到CoordinatorLayout
的间接子节点。
layout_anchor
only 与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;
}
这是一个已知的错误,请参阅https://code.google.com/p/android/issues/detail?id=220250
你应该升级到24.2.1版本,这个问题已经解决了。