Android.graphics.drawable.ColorDrawable cannot be added to android.support.v7.widget.RoundRectDrawableWithShadow file

I tried using CardView in my application, which worked very well in my xml layout. Since I want to generate them in my code, and not through xml, I tried to set the radius using CardView.setRadius () - a method suggested by Android developers ( https://developer.android.com/preview/material/ui-widgets. html # cardview ).

This does not work! I hope someone can help me with my problem or get a good workaround with XML layouts.

This is my code:

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()); float radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics()); for (String color : colors) { CardView card = new CardView(getActivity()); TextView text = new TextView(getActivity()); card.setBackgroundColor(Color.parseColor("#"+color)); card.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, height)); card.setRadius(radius); //Error!! text.setText(args.get(colors.indexOf(color)) + " - #" + color); card.addView(text); group.addView(card); } 

This is my journal:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brobox.materialcolorpalette/com.brobox.materialcolorpalette.MainActivity}: java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5146) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow at android.support.v7.widget.CardViewEclairMr1.setRadius(CardViewEclairMr1.java:76) at android.support.v7.widget.CardView.setRadius(CardView.java:89) at com.brobox.materialcolorpalette.ColorFragment.onCreateView(ColorFragment.java:61) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171) at android.app.Activity.performStart(Activity.java:5241) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:136)            at android.app.ActivityThread.main(ActivityThread.java:5146)            at java.lang.reflect.Method.invokeNative(Native Method)            at java.lang.reflect.Method.invoke(Method.java:515)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)            at dalvik.system.NativeStart.main(Native Method) 

UPDATE: I am using the new Lollipop SDK now with Appcompat version 21. Now it does not work at all when I try it without setRadius ().

New magazine:

 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:148) at android.support.v7.widget.CardViewEclairMr1.getMinWidth(CardViewEclairMr1.java:139) at android.support.v7.widget.CardView.onMeasure(CardView.java:181) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 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:16521) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1226) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.widget.ScrollView.onMeasure(ScrollView.java:326) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16521) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:848) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 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:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552) at android.view.View.measure(View.java:16521) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5146) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) at dalvik.system.NativeStart.main(Native Method) 

Update 2: I assume this is a Google bug. So I pointed them out to them: https://code.google.com/p/android/issues/detail?id=77843

+48
android android-cardview appcompat
Oct. 16 '14 at 8:31
source share
6 answers

I get this error when calling the setBackgroundColor () method, which is defined in the View class.

 cardView.setBackgroundColor(Color.parseColor("#ffffcc")); 

Instead, call the Card method BackgroundColor (), which is specific to CardView and is defined in the CardView class.

This code sets both the background color and the corner radius for me:

 cardView.setCardBackgroundColor(Color.parseColor("#ffffcc")); cardView.setRadius(50); 
+100
Jan 29 '15 at 0:40
source share

I found that you cannot set the map background. Even in XML. So this is my solution:

Put everything in the child map layout:

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:elevation="10dp"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/holder_relative_layout" android:orientation="horizontal" android:gravity="center_vertical"> ... 

Then in the code:

 cardView.row.findViewById(R.id.holder_relative_layout).setBackgroundColor(getResources().getColor(R.color.MediumSeaGreen)); 
+4
Dec 14 '14 at 22:35
source share

I know this is an old question, but if you want to change bgcolor CardView now in android.support.v7.widget.CardView, you can just use setCardBackgroundColor :

  cardView.setCardBackgroundColor(Color.parseColor("#dddddd")); 

or simply

  cv.setCardBackgroundColor(Color.LTGRAY); 
+4
May 14 '15 at 2:33
source share

The comment on using SetCardBAckgroundColor with CardViews correct, but I had to drop Views on the View to force it to compile

 ((CardView)holder.itemView).setCardBackgroundColor(Color.parseColor("#FFFFCC")); 

Everything is working now.

+1
Oct. 16 '15 at 7:47
source share

I faced the same crash. The problem is that you set the CardView background color programmatically to your code.

 card.setBackgroundColor(Color.parseColor("#"+color)); 

comment out the line. and set the background and other property in xml. Do not do this programmatically.

This worked for me. :)

You better define a different XML file for different situations / data that you want to distinguish.

0
Nov 14 '14 at 6:02
source share

This is a simple problem in the support library if you first set the wallpaper and then color. They (Google) call the getDrawable () method to set the background color if you cause a color change. They overlay Drawable on RoundRectdrawable, and you may have a different view of Drawable. For example, 9patch Drawable. Just override setBackgroundColor () to setBackgroundDrawable (new RoundedDrawable (...)) Not sure about the constructor and class name, just look at the map source code.

This is the problem:

 @Override public void setBackgroundColor(CardViewDelegate cardView, int color) { ((RoundRectDrawable) (cardView.getBackground())).setColor(color); } 

change to:

 @Override public void setBackgroundColor(int color) { setBackgroundDrawable(new RoundRectDrawable(color, getRadius())); } 
0
May 31 '16 at 2:53
source share



All Articles