我需要使用旋轉功能進行圖像視圖。所以我看着Android開發者site。並使用他們的代碼。但不知何故,我得到一個錯誤。ImageView在Android中的旋轉
錯誤:java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
我有這些代碼:
ImageView refresh = (ImageView)findViewById(R.id.refresh_devices_button);
refresh.setBackgroundResource(R.drawable.spin_animation); // The IDE says that it may produce null pointer exception
AnimationDrawable frameAnimation = (AnimationDrawable) refresh.getBackground();
frameAnimation.start();
在spin_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/scan_1" android:duration="50" />
<item android:drawable="@drawable/scan_2" android:duration="50" />
<item android:drawable="@drawable/scan_3" android:duration="50" />
<item android:drawable="@drawable/scan_4" android:duration="50" />
<item android:drawable="@drawable/scan_5" android:duration="50" />
</animation-list>
</selector>
請幫助我。從android的網站我得到的代碼,但他們的代碼不起作用。也許問題出在我的spin_animation.xml文件中。
refresh.getBackground
返回StateListDrawable
我想。
對不起,我編輯了我的答案.. – KrishnaJ