2015-05-15 55 views
3

我正在使用最新版本的支持庫22.1.1。無法轉換爲LayerDrawable(升級v7後)

我去,如:

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

,但升級後,在第2行崩潰了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) 

我在Android 4.2.2測試。任何提示&解決方法?

回答

1

我發現getProgressDrawable()沒有返回我的LayerDrawable了。

我設置一個LayerDrawable背景通過樣式,搭配:

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

由於v21.1添加的RatingBarAppCompatRatingBar色調感知的版本,他們現在ratingBarStyle讀取屬性,而不是android:ratingBarStyle。所以我不得不更換上述行:

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

什麼是myRatingBarStyle?請填寫你的答案 –

0

當你使用程序兼容性庫你的很多款式還沒有到包括機器人:,所以上面的代碼具有以下格式:

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

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

基本上你需要爲V21.1和更高版本以及兼容性設計風格,如果你正在設計幾個對象。

1

請嘗試這一個進度

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

5

我有同樣的問題的一套顏色。我搜索了它,發現如果我們寫入

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

在XML文件中。比起Android,默認將RatingBar轉換爲android.support.v7.widget.AppCompatRatingBar而不是android.widget.RatingBar


只需使用下面的代碼修改您的代碼,問題就會解決。

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