Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Using lparams in CardView's direct children causes ClassCastException #269

Closed
SuhairZain opened this issue Nov 5, 2016 · 2 comments
Closed

Comments

@SuhairZain
Copy link

SuhairZain commented Nov 5, 2016

Hi,
I have a DSL similar to the following:

cardView {
  cardElevation = dip(6).toFloat()
  useCompatPadding = true

  relativeLayout {
    textView {
      text = "TESTING"
    }.lparams { margin = dip(8) }
  }.lparams { width = matchParent } //Problematic line
}.lparams { width = matchParent ; centerInParent() }

But this throws the following exception:

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
at android.widget.FrameLayout.onMeasure(FrameLayout.java:195)
at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
at android.view.View.measure(View.java:18788)

Because of this line https://android.googlesource.com/platform/frameworks/support/+/master/v7/cardview/src/android/support/v7/widget/CardView.java#211 that calls super.onMeasure(widthMeasureSpec, heightMeasureSpec); which in turn calls https://chromium.googlesource.com/android_tools/+/master/sdk/sources/android-23/android/widget/FrameLayout.java#195 or final LayoutParams lp = (LayoutParams) child.getLayoutParams();

Here, the RelativeLayout.LayoutParams is typecasted to FrameLayout.LayoutParams which causes the exception. IIRC, the LayoutParams is choosen by Anko depending on the parent which should have been FrameLayout.LayoutParams for a CardView but is chosen as RelativeLayout.LayoutParams. For the time being, I've found a workaround which is given below.

cardView {
  cardElevation = dip(6).toFloat()
  useCompatPadding = true

  relativeLayout {
    textView {
      text = "TESTING"
    }.lparams { margin = dip(8) }
  }.layoutParams = FrameLayout.LayoutParams( //Explicitly create a FrameLayout.LayoutParams
    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT
  )
}.lparams { width = matchParent ; centerInParent() }

I'd like to know if this is a bug in Anko or if I'm doing something wrong. Please advice.

@SuhairZain SuhairZain changed the title Using lparams causes ClassCastException Using lparams in CardView's direct children causes ClassCastException Nov 5, 2016
@yanex
Copy link
Member

yanex commented Feb 22, 2017

The erroneous lparams() from the RelativeView is now forbidden by DslMarker annotation.
The problem now is the lack of lparams() inside the CardView.

@4u7
Copy link
Contributor

4u7 commented Nov 14, 2017

Fixed with 7c64c4b

@4u7 4u7 added this to the Anko 0.10.3 milestone Nov 14, 2017
@4u7 4u7 added the fixed label Nov 14, 2017
@4u7 4u7 closed this as completed Nov 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants