简体   繁体   English

Android RatingBar:getProgressDrawable不会转换为正确的类

[英]Android RatingBar: getProgressDrawable doesn't cast to right Class

I am using 我在用

Android RatingBar change star colors Android RatingBar更改明星颜色

This line of code 这行代码

    LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable();

works fine on my Nexus 4 emulator API v21. 在我的Nexus 4仿真器API v21上运行正常。 When trying the same code on my Galaxy S4 API v19, I get the following error message: 在我的Galaxy S4 API v19上尝试相同的代码时,我收到以下错误消息:

 Caused by: java.lang.ClassCastException: android.support.v7.internal.widget.TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable

What is happening? 怎么了?

I am building with the latest build tools 22, my target API is 22 is as well. 我正在构建最新的构建工具22,我的目标API也是22。

What happens is clearly explained in the error message : 错误消息中清楚地解释了会发生什么:

TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable

The support lib creates a wrapper in order to handle retro compatibility, you just need to take it into account. 支持lib创建一个包装器以处理复古兼容性,您只需要考虑它。 Have a look at the lib implementation, it probably wraps around a LayerDrawable . 看一下lib实现,它可能包含了一个LayerDrawable

You will need to do something along the lines of : 你将需要做一些事情:

LayerDrawable stars = getProgressDrawable(rating_bar);
}

private LayerDrawable getProgressDrawable(RatingBar ratingBar) {
  if (Build.VERSION >= LOLLIPOP) {
    return (LayerDrawable) ratingBar.getProgressDrawable();
  } else {
    // fetch the LayerDrawable based on Google's support implementation,  
    // probably a simple findViewById()
  }
}

LayerDrawable在API v21中使用。

Creating a subclass of RatingDialog and using that instead of android.widget.RatingBar worked for me. 创建RatingDialog的子类并使用它而不是android.widget.RatingBar为我工作。 Here is the code I used. 这是我使用的代码。

Subclass: 子类:

public class RatingBar extends android.widget.RatingBar {
public RatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public RatingBar(Context context, AttributeSet attrs) {
    super(context, attrs);
}
}

Then in my layouts: 然后在我的布局中:

 <your.package.RatingBar
    android:id="@+id/rating_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    />

Then in the classes where you are casting the class, just use your.package.RatingBar instead of android.widget.RatingBar 然后在你正在your.package.RatingBar类的类中,只需使用your.package.RatingBar而不是android.widget.RatingBar

Came accross the same issue today when creating a RatingBar programmatically. 今天在以编程方式创建RatingBar时遇到了同样的问题。 The following workaround is working for me also on Android 4.x devices using the AppCompatRatingBar: 以下解决方法也适用于使用AppCompatRatingBar的Android 4.x设备:

AppCompatRatingBar rb = new AppCompatRatingBar(activity);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
rb.setLayoutParams(layoutParams);
rb.setStepSize(1.0f);
rb.setMax(5);

LayerDrawable layerDrawable = null;
if (rb.getProgressDrawable() instanceof LayerDrawable) {
    layerDrawable = (LayerDrawable) rb.getProgressDrawable();
} else if (rb.getProgressDrawable() instanceof DrawableWrapper) {
    DrawableWrapper wrapper = (DrawableWrapper) rb.getProgressDrawable();
    if (wrapper.getWrappedDrawable() instanceof LayerDrawable) {
        layerDrawable = (LayerDrawable) wrapper.getWrappedDrawable();
    }
}

if (layerDrawable != null) {
    layerDrawable.getDrawable(2).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
    layerDrawable.getDrawable(0).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
    layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(activity, R.color.primary), PorterDuff.Mode.SRC_ATOP);
}

Please try this trick, It will work fine by adding getCurrent() method to return a drawable. 请尝试这个技巧,通过添加getCurrent()方法返回一个drawable它将工作正常。

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); RatingBar ratingBar =(ratingBar)findViewById(R.id.ratingBar);

LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable().getCurrent(); LayerDrawable stars =(LayerDrawable)ratingBar.getProgressDrawable()。getCurrent();

stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(2).setColorFilter(getResources()。getColor(R.color.colorPrimary),PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(getResources()。getColor(R.color.white),PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(getResources()。getColor(R.color.colorPrimary),PorterDuff.Mode.SRC_ATOP);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM