1

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.

John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71

1 Answers1

1

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

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • But why doesn't the error always manifest itself when using the app and only at random times? – John Ernest Guadalupe Dec 27 '13 at 07:26
  • @JohnErnestGuadalupe didn't get what you are saying..can you explain clearly?? – kalyan pvs Dec 27 '13 at 07:27
  • OP doesnt mention ImageView at all, so why do you do so? – pskink Dec 27 '13 at 07:27
  • Yes I am actually using an ImageView wherein I am setting the animation-list. What I am asking is actually why am I getting the error above wherein the BitmapDrawable cannot be cast to an AnimationDrawable where in actuality it works fine most of the time and then it crashes showing the error only sometimes. Shouldn't it crash more frequently considering your answer? – John Ernest Guadalupe Dec 27 '13 at 07:32
  • @JohnErnestGuadalupe is this exception is sometimes?? – kalyan pvs Dec 27 '13 at 07:37
  • @JohnErnestGuadalupe check this you will get clear idea http://stackoverflow.com/questions/12001793/android-imageview-setimagebitmap-vs-setimagedrawable what they are doing internally after your image set.. – kalyan pvs Dec 27 '13 at 07:37
  • @kalyanpvs what process are you talking about? when you call View.setBackgroundDrawable(Drawable) View doesnt change input Drawable at all – pskink Dec 27 '13 at 07:47
  • Yes the exception only happens sometimes. – John Ernest Guadalupe Dec 27 '13 at 07:47
  • @pskink you are right..sorry. this is only for ImageViews not for all views.For Views Actual Drawable will set.. – kalyan pvs Dec 27 '13 at 07:51
  • @JohnErnestGuadalupe post your code, if you have cast exception it means that you set the background of your View to BitmapDrawable, there is **NO** automatic background replacement – pskink Dec 27 '13 at 07:52
  • @pskink what you have said is correct for other views but when you set for ImageView they will change..once check this ImageView source code..http://stackoverflow.com/questions/12001793/android-imageview-setimagebitmap-vs-setimagedrawable – kalyan pvs Dec 27 '13 at 07:55
  • @kalyanpvs when you use ImageView.setImageDrawable(Drawable), too, ImageView doesnt change input Drawable at all – pskink Dec 27 '13 at 07:56
  • For not ImageView.setImageDrawable(Drawable) ther you are directly set the drawable object..this is for setImageBitmap(Bitmap bm) or setImageUri(Uri) – kalyan pvs Dec 27 '13 at 07:58
  • @pskink ImageView Source code http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/ImageView.java – kalyan pvs Dec 27 '13 at 08:00
  • you said "Whenever you set a bitmap or drawable to ImageView for recycling purpose android will create new BitmapDrawable object" which is not true: only when you call setImageBitmpap(Bitmap) a new BitmapDrawable is created – pskink Dec 27 '13 at 08:03