我需要更改星星ratingBar
的顏色,但會得到一個異常,它爲什麼會發生並且完全被接受?爲什麼我不能施放?
ResultActivity
private void setDados() {
TextView text = (TextView) findViewById(R.id.textView3);
text.setText((text.getText()) + "" + corretas);
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
ratingBar.setRating((100*corretas)/(corretas+erradas));
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
try {
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
}catch(Exception e){
e.printStackTrace();
}
}
登錄
04-18 22:02:39.828 31732-31732/com.morais.daniela.doctorquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.morais.daniela.doctorquiz/com.morais.daniela.doctorquiz.Activity.ResultActivity}: java.lang.ClassCastException: android.support.v7.internal.widget.TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4448)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v7.internal.widget.TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
at com.morais.daniela.doctorquiz.Activity.ResultActivity.setDados(ResultActivity.java:44)
at com.morais.daniela.doctorquiz.Activity.ResultActivity.onCreate(ResultActivity.java:36)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4448)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
UPDATE:這不會在API 5.0和更高的工作,我如何在冰淇淋和更高的作品嗎?無需創建星形圖像
發佈「ResultActivity」的所有代碼。加上你的錯誤似乎是:'android.support.v7.internal.widget.TintDrawableWrapper不能轉換爲android.graphics.drawable.LayerDrawable'。 –
類似的問題和答案:http://stackoverflow.com/a/17267921/950427。 –
第44行是'LayerDrawable stars =(LayerDrawable)ratingBar.getProgressDrawable();'。根據另一個問題中的評論不適用於API 5.0和起來http://stackoverflow.com/questions/2446270/android-ratingbar-change-star-colors –