我现在正在编写一个程序来在 android 应用程序中使用 AnimationDraawable 播放图像列表。图像列表是从服务器下载的。但是我在我的程序中遇到了无法将drawable添加到AnimationDrawable的错误。谁能帮我?
这是我的 logCat 捕获:
04-26 00:45:07.260: E/AndroidRuntime(17422): FATAL EXCEPTION: AsyncTask #2
04-26 00:45:07.260: E/AndroidRuntime(17422): java.lang.RuntimeException: An error occured while executing doInBackground()
04-26 00:45:07.260: E/AndroidRuntime(17422): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-26 00:45:07.260: E/AndroidRuntime(17422): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.lang.Thread.run(Thread.java:856)
04-26 00:45:07.260: E/AndroidRuntime(17422): Caused by: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
04-26 00:45:07.260: E/AndroidRuntime(17422): at com.tlab.shopping.Itemview$itemanimation.doInBackground(Itemview.java:1036)
04-26 00:45:07.260: E/AndroidRuntime(17422): at com.tlab.shopping.Itemview$itemanimation.doInBackground(Itemview.java:1)
04-26 00:45:07.260: E/AndroidRuntime(17422): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-26 00:45:07.260: E/AndroidRuntime(17422): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-26 00:45:07.260: E/AndroidRuntime(17422): ... 5 more
这是我的一段代码:
public static class itemanimation extends AsyncTask<String, Void, AnimationDrawable>{
public Itemview myActivity;
public itemanimation(Itemview a)
{
myActivity = a;
}
protected void onPreExecute(){
//text.setText("load");
}
protected AnimationDrawable doInBackground(String... x){
Bitmap bmImg = null;
URL myFileUrl =null;
String code = x[0];
String cur_url = null;
ImageView im = (ImageView) myActivity.findViewById(R.id.imageView1);
AnimationDrawable anim = null;
Object ob = im.getBackground();
anim = (AnimationDrawable) ob;
for (int frameIndex = 0; frameIndex < 4; frameIndex++) {
cur_url = "http://137.189.97.18/image/" + code + frameIndex +".jpg";
try {
myFileUrl= new URL(cur_url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
int[] bitmapData =new int[length];
byte[] bitmapData2 =new byte[length];
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
Bitmap bmp = Bitmap.createScaledBitmap(bmImg, 150, 150,true);
Drawable bmpImgDrawable = Drawable.createFromStream(is, code + frameIndex + ".jpg");
anim.addFrame(bmpImgDrawable, 250);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
anim.setOneShot(false);
return anim;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(AnimationDrawable z){
ImageView image = (ImageView) myActivity.findViewById(R.id.imageView1);
image.setBackgroundColor(00000000);
//image.setImageBitmap(bmp);
image.setBackgroundDrawable(z);
z.stop();
z.start();
}
}