【问题标题】:ClassCastException: AbsListView$LayoutParams cannot be cast to Gallery$LayoutParamsClassCastException:AbsListView$LayoutParams 无法转换为 Gallery$LayoutParams
【发布时间】:2012-10-24 07:05:50
【问题描述】:

我正在尝试开发一个 Android 3.1 平板电脑应用程序。

这个应用会有很多图片,我已经按照Processing Bitmaps Off the UI Thread教程做了,但是我做错了,因为我得到了:

java.lang.ClassCastException:android.widget.AbsListView$LayoutParams 无法转换为 android.widget.Gallery$LayoutParams

这是我的代码:

我在 Activity 上设置了图库

mFactGallery = (Gallery)mView.findViewById(R.id.factGallery);
mFactGallery.setAdapter(new ImageGalleryAdapter(mActivity, ImageView.ScaleType.FIT_END, 180, 90));

ImageGalleryAdapter.java

public class ImageGalleryAdapter extends BaseAdapter
{
    private ArrayList<String> mImagesPath;
    private Context mContext;
    private int mWidth;
    private int mHeight;

    public ArrayList<String> getmImagesPath()
    {
        return mImagesPath;
    }

    public void setmImagesPath(ArrayList<String> mImagesPath)
    {
        this.mImagesPath = mImagesPath;
    }

    public void addImage(String imagePath)
    {
        mImagesPath.add(imagePath);
    }

    public ImageGalleryAdapter(Context context, ImageView.ScaleType scaleType, int width, int height)
    {
        mContext = context;
        mWidth = width;
        mHeight = height;
        mScaleType = scaleType;

        mImagesPath = new ArrayList<String>();
    }

    @Override
    public int getCount()
    {
        return mImagesPath.size();
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // Get a View to display image data                     
        ImageView imageView;
        // if it's not recycled, initialize some attributes
        if (convertView == null)
        {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        }
        else
        {
            imageView = (ImageView) convertView;
            // Recicla el Bitmap.
            Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            if (bm != null)
                bm.recycle();
        }

        String filePath = mImagesPath.get(position);
        if (BitmapTools.cancelPotentialWork(filePath, imageView))
        {
            String[] params = {filePath, Integer.toString(mWidth), Integer.toString(mHeight)};
            final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
            final AsyncDrawable asyncDrawable =
                    new AsyncDrawable(mContext.getResources(), filePath, task);
            imageView.setImageDrawable(asyncDrawable);
            task.execute(params);
        }

        return imageView;
    }
}

BitmapWorkerTask.java

public class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
    private final WeakReference<ImageView> imageViewReference;
    public String imgPath = "";

    public BitmapWorkerTask(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params)
    {
        imgPath = params[0];
        int width = Integer.valueOf(params[1]).intValue();
        int height = Integer.valueOf(params[2]).intValue();
        return BitmapTools.decodeSampledBitmapFromDisk(imgPath, width, height);
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap)
    {
        if (isCancelled())
        {
            bitmap = null;
        }

        if (imageViewReference != null && bitmap != null)
        {
            final ImageView imageView = imageViewReference.get();
            final BitmapWorkerTask bitmapWorkerTask =
                    BitmapTools.getBitmapWorkerTask(imageView);
            if (this == bitmapWorkerTask && imageView != null)
            {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

AsyncDrawable.java

public class AsyncDrawable extends BitmapDrawable
{
    private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;

    public AsyncDrawable(Resources res, String filepath,
            BitmapWorkerTask bitmapWorkerTask)
    {
        super(res, filepath);
        bitmapWorkerTaskReference =
            new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
    }

    public BitmapWorkerTask getBitmapWorkerTask()
    {
        return bitmapWorkerTaskReference.get();
    }
}

我做错了什么?

【问题讨论】:

    标签: android android-asynctask android-gallery


    【解决方案1】:

    GridView.LayoutParams替换为Gallery.LayoutParams如下,这将解决您的问题

    imageView.setLayoutParams(new Gallery.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    

    【讨论】:

    【解决方案2】:

    在 ImageGalleryAdapter.java 中,这是错误的:

    imageView.setLayoutParams(new GridView.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    

    使用正确的LayoutParams类,应该是

    imageView.setLayoutParams(new Gallery.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    

    (LayoutParams 的超类是Gallery 而不是GridView

    【讨论】:

    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    相关资源
    最近更新 更多