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

时间:2015-06-20 18:39:02

标签: android android-support-library android-cardview

我遇到的问题基本上是answer中的内容,但我仍会在下面解释。我正在使用android 5.0.1和另一个4.2.2的设备上进行测试。在我的5.0.1设备上,我没有错误,一切都按预期运行。在我的4.2.2设备上,我收到以下错误:

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)

此行发生错误:card1.setBackgroundColor(getResources().getColor(R.color.White));

现在我已尝试使用setCardBackgroundColor链接的答案,但这会导致我的设备上产生错误。有什么建议吗?

我尝试使用的CardView是android.support.v7.widget.CardView

这是产生错误的布局:

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

阅读issue described here后,我注意到它已被关闭。据我所知,我使用的是最新版本的平台工具和sdk工具,我仍然有这个问题。

经过一些进一步测试后,我发现我使用的模拟器没有GooglePlayServices。我目前无权访问低于Android 6.0的物理设备进行测试。是否有可能不使用GooglePlayServices导致此问题?

7 个答案:

答案 0 :(得分:2)

这对我来说是循环视图中的cardview

 ((CardView)viewholder.itemView).setCardBackgroundColor(Color.WHITE);

答案 1 :(得分:2)

你也可以通过给予他们想要的东西来保持两个版本的android快乐,而不是试图强迫和施展某些东西:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
       // below lillipop
       view.setCardBackgroundColor(Color.WHITE);
       // working for lower questions as per another answer posted here
   } else {
        // lollipop and above
        card1.setBackgroundColor(getResources().getColor(R.color.White));
        // known to be working for lillipop as per your question
}

基本上为每个Android版本调用不同的工作方法

要正确识别棒棒糖上方和下方的Android版本,请使用LILLIPOP_MR1Soruce

答案 2 :(得分:1)

您可以使用:

card1.setBackgroundColor(Color.rgb(255, 255, 255));

而不是:

card1.setBackgroundColor(getResources().getColor(R.color.White));

答案 3 :(得分:1)

我想,最简单的方法就是使用彩色滤镜。我在4.2.2上尝试了这个,它运行正常。

cardView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);

答案 4 :(得分:1)

使用setBackgroundColor(int color)实际上是在View上调用方法,切换背景。 CardView需要RoundRectDrawableWithShadow,您刚刚覆盖了CardView view = (CardView) findViewById(R.id.card); view.setCardBackgroundColor(Color.RED); 。因此例外。

cardView.setCardBackgroundColor(int)

务必致电> plot(x, y, type = "n", xlab = "", ylab = "", axes = F) 并确保其有效。它适用于4.2.2,通过查看cardView源代码我也对其他api充满信心。如果这也导致崩溃,那必然是另一个原因。

答案 5 :(得分:1)

可能通过使用PublicRoundRectDrawableWithShadow解决了这个问题 创建PublicRoundRectDrawableWithShadow。

class HomeController{
@RequestMapping(value="index")
public void home(){
}
}

class DemoController{
@RequestMapping(value="index")
public void demo(){
}
}

package android.support.v7.widget; import android.content.res.Resources; public class PublicRoundRectDrawableWithShadow extends RoundRectDrawableWithShadow { public PublicRoundRectDrawableWithShadow(Resources resources, CardView cardView, int backgroundColor) { super(resources, backgroundColor, cardView.getRadius(), cardView.getCardElevation(), cardView.getMaxCardElevation()); } } 就像这样。

setBackground()

答案 6 :(得分:0)

请尝试在特定行下方设置background color的{​​{1}}:

card view object