ClassCastException:android.support.v4.graphics.drawable.DrawableWrapperHoneycomb无法强制转换为android.graphics.drawable.LayerDrawable

时间:2015-09-07 12:22:35

标签: android classcastexception ratingbar

我正在尝试更改RatingBar星级颜色,但在迁移到Android API 23后,我的以下代码无效并给我一个例外。

LayerDrawable ratingBarStars = (LayerDrawable) holder.ratingBar.getProgressDrawable();
        ratingBarStars.getDrawable(0).setColorFilter(mContext.getResources()
                                                             .getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);
        ratingBarStars.getDrawable(1).setColorFilter(mContext.getResources()
                                                             .getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);
        ratingBarStars.getDrawable(2).setColorFilter(mContext.getResources()
                                                             .getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);

错误是:

java.lang.ClassCastException: android.support.v4.graphics.drawable.DrawableWrapperHoneycomb cannot be cast to android.graphics.drawable.LayerDrawable

2 个答案:

答案 0 :(得分:3)

Selvin在评论部分中的

DrawableCompat.setTint(ratingBar.getProgressDrawable(),color);是正确的答案。请接受答案或要求他提供答案并接受

答案 1 :(得分:0)

请参阅https://stackoverflow.com/a/38207513/570168

TL; DR:

 layerDrawable = (LayerDrawable) rb.getProgressDrawable().getWrappedDrawable();