Cannot cast to LayerDrawable anymore (after upgrading v7)

♀尐吖头ヾ 提交于 2019-12-12 12:33:12

问题


I'm using last version of the support libraries, 22.1.1.

I used to go like:

mRatingBar = (RatingBar) getActivity().findViewById(R.id.rating);
LayerDrawable layer = (LayerDrawable) mRatingBar.getProgressDrawable();

but after upgrading it crashes at line 2 with a ClassCastException:

android.support.v4.graphics.drawable.DrawableWrapperHoneycomb cannot be cast to android.graphics.drawable.LayerDrawable
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:725)

I'm testing on Android 4.2.2. Any hints & workaround?


回答1:


I got same issue. I searched for it and found that, If we write

<RatingBar
android:layout_width="wrap_content"
android:numStars="5"
android:layout_height="wrap_content"
/>

in XML file. Than, Android by default convert RatingBar into, android.support.v7.widget.AppCompatRatingBar instead of android.widget.RatingBar.


Just modify your code with below code and the issue will get solved.

<android.widget.RatingBar
    android:layout_width="wrap_content"
    android:numStars="5"
    android:layout_height="wrap_content"
    />



回答2:


I found out that getProgressDrawable() was not returning my LayerDrawable anymore.

I was setting a LayerDrawable background through style, with:

<item name="android:ratingBarStyle">@style/myRatingBarStyle</item>

Since v21.1 added a tint-aware version of the RatingBar, AppCompatRatingBar, they are now reading attributes from ratingBarStyle, rather than android:ratingBarStyle. So I had to replace the above line with:

<item name="ratingBarStyle">@style/myRatingBarStyle</item>
<item name="android:ratingBarStyle">@style/myRatingBarStyle</item> //API21+



回答3:


Please try this one for set color of progressbar

DrawableCompat.setTint(ratingBar.getProgressDrawable(),color);




回答4:


When you're using AppCompat libraries many of your styles have not to include the android:, so the code above has this format:

<!-- Customize your theme here. -->
<item name="android:ratingBarStyle">@style/myRatingBarStyle</item>
···

<!-- Support library compatibility -->
<item name="ratingBarStyle">@style/myRatingBarStyle</item> 
···

Basically you need to style for V21.1 and above and for compatibility, if you're styling several objects.



来源:https://stackoverflow.com/questions/30266334/cannot-cast-to-layerdrawable-anymore-after-upgrading-v7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!