android.graphics.drawable.ColorDrawable无法强制转换为android.support.v7.widget.RoundRectDrawableWithShadow

时间:2014-10-16 08:31:59

标签: android android-appcompat android-cardview

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

这根本不起作用!我希望有人可以帮助我,解决我的问题或者使用xml布局得到很好的解决方法。

这是我的代码:

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

这是我的日志:

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)

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

新日志:

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)

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

6 个答案:

答案 0 :(得分:102)

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

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

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

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

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

答案 1 :(得分:6)

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

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

或只是

 cv.setCardBackgroundColor(Color.LTGRAY);

答案 2 :(得分:4)

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

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

<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">
...

   

然后在代码中:

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

答案 3 :(得分:1)

关于将SetCardBAckgroundColorCardViews一起使用的评论是正确的,但我必须将视图转换为View并将其编译为

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

现在一切正常。

答案 4 :(得分:0)

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

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

评论该行。并在xml本身中设置背景和其他属性。不要以编程方式进行。

这对我有用。 :)

最好为不同的情境/数据定义不同的xml文件,以区分它们。

答案 5 :(得分:0)

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

这是问题所在:

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

更改为:

@Override
public void setBackgroundColor(int color) {
    setBackgroundDrawable(new RoundRectDrawable(color, getRadius()));
}