“Class Cast Exception” when trying to set image frames to background

偶尔善良 提交于 2019-12-13 06:13:04

问题


I am trying to make my RelativeLayout background image animate-able by trying to set number of image frames.

Here is how I set the animation

private View fullView;
fullView = (View)findViewById(R.id.fullView);

Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.eng_anim_1);
            drawable.setAlpha(100);

            fullView.setBackgroundDrawable(drawable);
            AnimationDrawable progressAnimation = (AnimationDrawable) fullView.getBackground();
            progressAnimation.start();

Here is my 'list of images"

<?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/eng_anim_1" android:duration="50" />
<item android:drawable="@drawable/eng_anim_2" android:duration="50" />
<item android:drawable="@drawable/eng_anim_3" android:duration="50" />
<item android:drawable="@drawable/eng_anim_5" android:duration="50" />
<item android:drawable="@drawable/eng_anim_6" android:duration="50" />
<item android:drawable="@drawable/eng_anim_7" android:duration="50" />
<item android:drawable="@drawable/eng_anim_8" android:duration="50" />
<item android:drawable="@drawable/eng_anim_9" android:duration="50" />
<item android:drawable="@drawable/eng_anim_10" android:duration="50" />
<item android:drawable="@drawable/eng_anim_11" android:duration="50" />
<item android:drawable="@drawable/eng_anim_12" android:duration="50" />
<item android:drawable="@drawable/eng_anim_13" android:duration="50" />
<item android:drawable="@drawable/eng_anim_14" android:duration="50" />
<item android:drawable="@drawable/eng_anim_15" android:duration="50" />
<item android:drawable="@drawable/eng_anim_16" android:duration="50" />
<item android:drawable="@drawable/eng_anim_17" android:duration="50" />
<item android:drawable="@drawable/eng_anim_18" android:duration="50" />
<item android:drawable="@drawable/eng_anim_19" android:duration="50" />
<item android:drawable="@drawable/eng_anim_20" android:duration="50" />

</animation-list>

But the problem is, I am getting the following error

08-04 19:03:51.582: E/AndroidRuntime(504): FATAL EXCEPTION: main
08-04 19:03:51.582: E/AndroidRuntime(504): java.lang.RuntimeException: Unable to start activity ComponentInfo{yohanSoftware.languageGame/yohanSoftware.languageGame.Game}: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.os.Looper.loop(Looper.java:123)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-04 19:03:51.582: E/AndroidRuntime(504):  at java.lang.reflect.Method.invokeNative(Native Method)
08-04 19:03:51.582: E/AndroidRuntime(504):  at java.lang.reflect.Method.invoke(Method.java:507)
08-04 19:03:51.582: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-04 19:03:51.582: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-04 19:03:51.582: E/AndroidRuntime(504):  at dalvik.system.NativeStart.main(Native Method)
08-04 19:03:51.582: E/AndroidRuntime(504): Caused by: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
08-04 19:03:51.582: E/AndroidRuntime(504):  at yo.lang.Game.setUI(Game.java:236)
08-04 19:03:51.582: E/AndroidRuntime(504):  at yo.lan.Game.onCreate(Game.java:127)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-04 19:03:51.582: E/AndroidRuntime(504):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-04 19:03:51.582: E/AndroidRuntime(504):  ... 11 more

I do not understand why I am getting this.


回答1:


It's because you are trying to cast BitmapDrawable(first frame of animation) to AnimationDrawable. Try like this:

Drawable drawable = res.getDrawable(R.drawable.<your_animation_xml_file_name>);


来源:https://stackoverflow.com/questions/18043504/class-cast-exception-when-trying-to-set-image-frames-to-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!