The issue I am having is basically what is found in this answer but I will still explain below. I am testing on a device with android 5.0.1 and another with 4.2.2. On my device with 5.0.1 I get no errors and everything behaves as expected. On my 4.2.2 device I get the following error:
java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
at android.support.v7.widget.CardViewEclairMr1.getShadowBackground(CardViewEclairMr1.java:159)
at android.support.v7.widget.CardViewEclairMr1.getMinWidth(CardViewEclairMr1.java:150)
at android.support.v7.widget.CardView.onMeasure(CardView.java:181)
at android.view.View.measure(View.java:15696)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261)
at android.view.View.measure(View.java:15696)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2225)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1290)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1527)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1190)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4860)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766)
at android.view.Choreographer.doCallbacks(Choreographer.java:575)
at android.view.Choreographer.doFrame(Choreographer.java:542)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)
with the error occurring at this line: card1.setBackgroundColor(getResources().getColor(R.color.White));
Now as suggested by the linked answer I have tried using setCardBackgroundColor
however this causes the error to be produced on both my devices. Any suggestions?
The CardView I am attempting to use is a android.support.v7.widget.CardView
This is the layout producing the error:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#bbbbb5"
android:orientation="vertical"
android:weightSum="100">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-65dp"
android:layout_toRightOf="@+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="@drawable/plane1" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
android:layout_alignRight="@+id/imageView"
android:layout_marginBottom="40dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_navigate_next_white_48dp" />
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="280dp"
android:clickable="true"
android:onClick="card1ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/choice_text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:onClick="card1ClickMethod"
android:text="Loret ipsum sample"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="340dp"
android:clickable="true"
android:onClick="card2ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/choice_text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:onClick="card2ClickMethod"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="400dp"
android:clickable="true"
android:onClick="card3ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/choice_text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:onClick="card3ClickMethod"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view4"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="460dp"
android:clickable="true"
android:onClick="card4ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/choice_text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:onClick="card4ClickMethod"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/resulttext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/progressBar1"
android:layout_alignParentStart="true"
android:layout_gravity="center_vertical|left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:textSize="15dp" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/fab"
android:layout_gravity="right|center_vertical"
android:layout_marginTop="20dp" />
<ImageView
android:id="@+id/prem_BACK"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:src="@drawable/background4"
android:visibility="gone" />
After reading through the issue described here I noticed that it has since been closed. As far as I am aware I am using the most updated version of the platform tools and sdk tools and I still have this issue.
After some further testing, I have discovered that the emulator I am using does not have GooglePlayServices. I do not currently have access to a physical device with less than Android 6.0 to test on. Is it a possibility that not having GooglePlayServices could cause this issue?
This is working for me for cardview inside recycleview
((CardView)viewholder.itemView).setCardBackgroundColor(Color.WHITE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With