Android RatingBar: getProgressDrawable doesn't cast to right Class

后端 未结 5 1100
耶瑟儿~
耶瑟儿~ 2021-01-21 06:37

I am using

Android RatingBar change star colors

This line of code

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


        
5条回答
  •  春和景丽
    2021-01-21 06:54

    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. Have a look at the lib implementation, it probably wraps around a 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()
      }
    }
    

提交回复
热议问题