Shijil Shijil - 3 months ago 39
Android Question

ClassCastException in FloatingActionButton

got ClassCastException in android.support.design.widget.FloatingActionButton after updating com.android.support:design lib to 24.2.0. its working fine in lib version 24.1.1

java.lang.ClassCastException: android.widget.FrameLayout$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)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2000)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)

sJy sJy
Answer

The issue seems to be latest Support Library version 24.2.0. Setting the anchor for FloatingButton to a view which is not a direct child of its parent CoordinatorLayout is causing this.

This is a known issue as per Android Open Source Project - Issue Tracker. You can see the issue marked as future release for 24.2.1 at FAB can no longer be anchored to indirect children of CoordinatorLayout.

Possible workaround from the comments in that reported issue are

  • Set the anchor view of FloatingActionButton to direct child of Coordinatorlayout.

OR

  • Wrap the View which you are setting as the anchor for FloatinActionButton inside a new CoordinatorLayout

From Android Open Source Project - Issue Tracker

Status: FutureRelease

Labels: Target-Support-24.2.1

Comments