Is there any way that an Animation-List xml set drawable suddenly becomes a BitmapDrawable?

1.2k views Asked by At

So i have this animation-list drawable and it is a series of images.

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> 
    <item android:drawable="@drawable/image1" android:duration="2000" /> 
    <item android:drawable="@drawable/image2" android:duration="2000" /> 
</animation-list>

Now the problem is, there are times when I would get this error and causes a force close of my app:

java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
at com.example.try.drawable.AnimationClass.run(AnimationClass.java:80)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
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:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)

Here is what I have is being pointed to in the AnimationClass:

public void run() {
    ((AnimationDrawable) mImages.getDrawable()).start();
}

The mImages variable is the ones I have in my xml. I am displaying them to the user much like a splashImage. The problem is I sometimes get this error and I don't know how it really happens.

P.S. I intentionally changed some variable names and package names, but hopefully the code is still as clear as it was for me.

1

There are 1 answers

15
kalyan pvs On

Whenever you set a bitmap to ImageView for recycling purpose android will create new BitmapDrawable object

BitmapDrawable drawable =new BitmapDrawable(bitmap);

and set it to the ImageView..because For recycling the Bitmap in the Imageview like..

drawable.getBitmap().recycle();

so thats why it is returning the BitmapDrawable object