I need to make an imageview with rotation feature. So I looked at the android developers site. And used their codes. But somehow I get an error.
Error:java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
I have these codes:
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();
In 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>
Please help me. From android's site I get the codes but their codes does not work. Maybe the problem is with my spin_animation.xml file.
refresh.getBackground
returns StateListDrawable
I think.
I found that android site is okay. The problem was with my xml file. I had this code
<?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>
Instead of the above code I used the following code. I had to remove the<selector>
tags.
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
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>