Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ClassCastException:AblistView$LayoutParams不能强制转换为Gallery$LayoutParams_Android_Android Asynctask_Android Gallery - Fatal编程技术网

Android ClassCastException:AblistView$LayoutParams不能强制转换为Gallery$LayoutParams

Android ClassCastException:AblistView$LayoutParams不能强制转换为Gallery$LayoutParams,android,android-asynctask,android-gallery,Android,Android Asynctask,Android Gallery,我正在尝试开发一款Android 3.1平板电脑应用程序 这个应用程序将有很多图像,我已经按照教程进行了操作,但我做了一些错误的事情,因为我得到了: java.lang.ClassCastException:android.widget.AblistView$LayoutParams不能强制转换为android.widget.Gallery$LayoutParams 这是我的代码: 我在活动中设置了图库 mFactGallery = (Gallery)mView.findViewById(R.i

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

这个应用程序将有很多图像,我已经按照教程进行了操作,但我做了一些错误的事情,因为我得到了:

java.lang.ClassCastException:android.widget.AblistView$LayoutParams不能强制转换为android.widget.Gallery$LayoutParams

这是我的代码:

我在活动中设置了图库

mFactGallery = (Gallery)mView.findViewById(R.id.factGallery);
mFactGallery.setAdapter(new ImageGalleryAdapter(mActivity, ImageView.ScaleType.FIT_END, 180, 90));
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);
            }
        }
    }
}
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();
    }
}
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;
    }
}
公共类ImageGalleryAdapter扩展了BaseAdapter
{
私人律师事务所;
私有上下文;
私人英特姆维兹;
私营机构;
公共阵列列表getmImagesPath()
{
返回mImagesPath;
}
public void setmImagesPath(数组列表mImagesPath)
{
this.mImagesPath=mImagesPath;
}
公共无效添加图像(字符串图像路径)
{
mImagesPath.add(图像路径);
}
公共ImageGalleryAdapter(上下文上下文,ImageView.ScaleType ScaleType,int-width,int-height)
{
mContext=上下文;
mWidth=宽度;
mHeight=高度;
mScaleType=scaleType;
mImagesPath=newarraylist();
}
@凌驾
public int getCount()
{
返回mImagesPath.size();
}
@凌驾
公共对象getItem(int位置)
{
返回位置;
}
@凌驾
公共长getItemId(int位置)
{
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
//获取显示图像数据的视图
图像视图图像视图;
//如果没有回收,初始化一些属性
if(convertView==null)
{
imageView=新的imageView(mContext);
imageView.setScaleType(imageView.ScaleType.CENTER\U裁剪);
imageView.setLayoutParams(新的GridView.LayoutParams(
LayoutParams.MATCH_父级,LayoutParams.MATCH_父级);
}
其他的
{
imageView=(imageView)convertView;
//循环位图。
位图bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
如果(bm!=null)
bm.recycle();
}
字符串filePath=mImagesPath.get(位置);
if(BitmapTools.cancelPotentialWork(文件路径、图像视图))
{
String[]params={filePath,Integer.toString(mWidth),Integer.toString(mHeight)};
最终BitmapWorkerTask任务=新的BitmapWorkerTask(imageView);
最终异步可绘制异步可绘制=
新的AsyncDrawable(mContext.getResources(),文件路径,任务);
setImageDrawable(asyncDrawable);
task.execute(params);
}
返回图像视图;
}
}
BitmapWorkerTask.java

mFactGallery = (Gallery)mView.findViewById(R.id.factGallery);
mFactGallery.setAdapter(new ImageGalleryAdapter(mActivity, ImageView.ScaleType.FIT_END, 180, 90));
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);
            }
        }
    }
}
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();
    }
}
公共类BitmapWorkerTask扩展异步任务
{
私有最终WeakReference imageViewReference;
公共字符串imgPath=“”;
公共位图工作任务(ImageView ImageView){
//使用WeakReference确保可以对ImageView进行垃圾收集
imageViewReference=新的WeakReference(imageView);
}
//在背景中解码图像。
@凌驾
受保护位图doInBackground(字符串…参数)
{
imgPath=params[0];
int width=Integer.valueOf(参数[1]).intValue();
int height=Integer.valueOf(参数[2]).intValue();
返回BitmapTools.decodeSampledBitmapFromDisk(imgPath,width,height);
}
//完成后,查看ImageView是否仍然存在并设置位图。
@凌驾
受保护的void onPostExecute(位图)
{
如果(isCancelled())
{
位图=空;
}
if(imageViewReference!=null&&bitmap!=null)
{
最终ImageView=imageViewReference.get();
最终位图工作任务位图工作任务=
getBitmapWorkerTask(imageView);
if(this==bitmapWorkerTask&&imageView!=null)
{
设置图像位图(位图);
}
}
}
}
AsyncDrawable.java

mFactGallery = (Gallery)mView.findViewById(R.id.factGallery);
mFactGallery.setAdapter(new ImageGalleryAdapter(mActivity, ImageView.ScaleType.FIT_END, 180, 90));
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);
            }
        }
    }
}
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();
    }
}
公共类AsyncDrawable扩展了BitmapDrawable
{
私有最终WeakReference位图工作区taskreference;
公共AsyncDrawable(资源、字符串文件路径、,
位图工作任务(位图工作任务)
{
super(res,filepath);
bitmapWorkerTaskReference=
新的WeakReference(位图工作任务);
}
公共BitmapWorkerTask getBitmapWorkerTask()
{
返回bitmapWorkerTaskReference.get();
}
}

我做错了什么?

在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

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

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

我们需要另一只眼睛来发现代码中的错误。谢谢你的回答。你能帮点忙吗?我们需要另一只眼睛来发现代码中的错误。谢谢你的回答。我也有同样的问题