使用自定义显示器时ClassCastException

时间:2013-03-18 13:28:39

标签: android universal-image-loader

我正在尝试在我的项目中使用UIL和gridview布局。出于测试目的,我已经修改了项目本身的代码。我的要求是显示一个TransitionDrawable,原始位图作为图层零中的基本图像和第一层中的刻度线。这是自定义BitmapDisplayer代码。

public class customBitmapDisplayer implements BitmapDisplayer
{

public static Drawable tick;
Drawable[] layers = new Drawable[2];
TransitionDrawable mytransition;

@Override
public Bitmap display(Bitmap bitmap, ImageView imageView)
{
BitmapDrawable base_image = new BitmapDrawable(imageView.getResources(), bitmap);
layers[0] = base_image;
layers[1] = tick;

mytransition = new TransitionDrawable(layers);
imageView.setImageDrawable(mytransition);
return bitmap;
}

public customBitmapDisplayer(Drawable tick_mark)
{
tick = tick_mark;
}
}

我还稍微修改了getView。这是代码。

if(selected[position])  //selected is a boolean array
{
TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();
quickTransition.startTransition(0);
}

顺便说一句,我正在使用UIL 1.8.0,默认的ImageConfiguration,DisplayImageOptions被修改为与自定义BitmapDisplayer一起使用,其代码在上面。我在android 2.3.4上测试并发现没有问题,但是在android 4.0.4上,我在getView方法中得到了一个ClassCastException,当我在“选择”每个图像后滚动浏览整个gridview时,BitmapDrawable无法转换为TransitionDrawable。

这是堆栈跟踪。

03-20 20:46:40.797: E/AndroidRuntime(11943): FATAL EXCEPTION: main
03-20 20:46:40.797: E/AndroidRuntime(11943): java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.TransitionDrawable
03-20 20:46:40.797: E/AndroidRuntime(11943):    at com.nostra13.example.universalimageloader.ImageGridActivity$ImageAdapter.getView(ImageGridActivity.java:219)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.AbsListView.obtainView(AbsListView.java:2019)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.makeAndAddView(GridView.java:1338)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.makeRow(GridView.java:343)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.fillUp(GridView.java:384)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.fillGap(GridView.java:260)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4567)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3834)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.os.Handler.handleCallback(Handler.java:605)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.os.Looper.loop(Looper.java:137)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.app.ActivityThread.main(ActivityThread.java:4448)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at java.lang.reflect.Method.invoke(Method.java:511)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at dalvik.system.NativeStart.main(Native Method)
03-20 20:46:44.377: I/Process(11943): Sending signal. PID: 11943 SIG: 9

ImageGridActivity.java在第219行中包含以下代码。

TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();

这是我的getView代码。

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    final ImageView imageView;
    if (convertView == null)
    {
        imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
    }
    else
    {
        imageView = (ImageView) convertView;
    }

    imageLoader.displayImage(imageUrls[position], imageView, options);

    if(selected[position])
    {

        TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();
        quickTransition.startTransition(0);

    }
    return imageView;
}

1 个答案:

答案 0 :(得分:0)

我认为您应该将getView()方法更改为以下内容:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    final ImageView imageView;
    if (convertView == null) {
        imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
    } else {
        imageView = (ImageView) convertView;
    }

    imageLoader.displayImage(imageUrls[position], imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if(selected[position]) {
                    TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();
                    quickTransition.startTransition(0);
                }
            }
        });
    return imageView;
}