android.graphics.drawable.ColorDrawable 不能转换为 android.support.v7.widget.RoundRectDrawableWithShadow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26399670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:21:02  来源:igfitidea点击:

android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow

androidandroid-appcompatandroid-cardview

提问by D4ddy-LiLd4rk

I tried to use CardView in my Application which worked pretty good within my xml Layout. Since I want to generate them in my code and not via xml I tried to set the radius with CardView.setRadius()-Method as suggested by Android Developers (https://developer.android.com/preview/material/ui-widgets.html#cardview).

我尝试在我的应用程序中使用 CardView,它在我的 xml 布局中运行良好。由于我想在我的代码中而不是通过 xml 生成它们,我尝试按照 Android 开发人员的建议使用 CardView.setRadius()-Method 设置半径(https://developer.android.com/preview/material/ui-widgets .html#cardview)。

This does not work at all! I hope someone can help me, with my problem or got a nice workaround with xml layouts.

这根本行不通!我希望有人可以帮助我解决我的问题,或者对 xml 布局有一个很好的解决方法。

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 Log:

这是我的日志:

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.access0(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.access0(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 use the new Lollipop SDK now with Appcompat version 21. Now It does not work at all even, when I try it without setRadius().

更新:我现在使用新的 Lollipop SDK 和 Appcompat 版本 21。现在它根本不起作用,当我在没有 setRadius() 的情况下尝试它时。

New Log:

新日志:

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 guess this is a bug by google. So I pointed it out for them: https://code.google.com/p/android/issues/detail?id=77843

更新 2:我猜这是谷歌的一个错误。所以我为他们指出:https: //code.google.com/p/android/issues/detail?id=77843

回答by Nicol Eye

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

调用在 View 类中定义的 setBackgroundColor() 方法时出现此错误。

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

Instead setCardBackgroundColor() method should be called, which is specific to CardViews and defined within CardView class.

相反,应调用set CardBackgroundColor() 方法,该方法特定于 CardViews 并在 CardView 类中定义。

This code setting both background colour and the corner radius works for me:

此代码设置背景颜色和角半径对我有用:

cardView.setCardBackgroundColor(Color.parseColor("#ffffcc"));
cardView.setRadius(50);

回答by rcorbellini

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

我知道这是一个老问题,但是,如果您现在想在 android.support.v7.widget.CardView 中更改 CardView 的 bgcolor,您可以使用setCardBackgroundColor

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

or just

要不就

 cv.setCardBackgroundColor(Color.LTGRAY);

回答by terrigenus

I found that you cannot set the background of a card view. Even in XML. So this is my workaround:

我发现您无法设置卡片视图的背景。即使在 XML 中。所以这是我的解决方法:

Put everything in a child layout of the card:

将所有内容放在卡片的子布局中:

<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 code:

然后在代码中:

cardView.row.findViewById(R.id.holder_relative_layout).setBackgroundColor(getResources().getColor(R.color.MediumSeaGreen));

回答by formica

The comment about using SetCardBAckgroundColorwith CardViewsis correct but I had to cast the Views to Viewas well to get it to compile

关于使用SetCardBAckgroundColorwith的评论CardViews是正确的,但我也必须将视图投射到View以使其编译

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

All works now.

现在一切正常。

回答by Milan Jurkulak

This is an easy problem of support library, if you set background drawable first and then color. They (Google) call getDrawable() to set the background color, if you call color Change. They cast Drawable to RoundRectdrawable and you possibly have another kind of Drawable. For example a ninepatch Drawable. Simply override setBackgroundColor() to setBackgroundDrawable(new RoundedDrawable(...)) Not sure about constructor and class name, just look at source code of cardview.

这是支持库的一个简单问题,如果您先设置背景可绘制,然后再设置颜色。他们(谷歌)调用 getDrawable() 来设置背景颜色,如果你调用 color Change。他们将 Drawable 转换为 RoundRectdrawable,您可能还有另一种 Drawable。例如一个 Ninepatch Drawable。只需将 setBackgroundColor() 覆盖为 setBackgroundDrawable(new RoundedDrawable(...)) 不确定构造函数和类名,只需查看 cardview 的源代码。

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()));
}

回答by vikoo

I faced the same crash. The problem is you are setting background color to CardView programatically in your code.

我遇到了同样的崩溃。问题是您在代码中以编程方式为 CardView 设置背景颜色。

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

comment the line. and set the background and other property in xml itself. Don't do it programatically.

注释该行。并在 xml 本身中设置背景和其他属性。不要以编程方式执行此操作。

This worked for me. :)

这对我有用。:)

Better you define the different xml file for different situation / data, that you want to differentiate.

最好为要区分的不同情况/数据定义不同的 xml 文件。