这个应用程序将有很多图像,我已经按照Processing Bitmaps Off the UI Thread教程,但我做错了,因为我得到:
java.lang.classCastException:androID.Widget.AbsListVIEw $LayoutParams无法强制转换为androID.Widget.gallery $LayoutParams
这是我的代码:
我在一个Activity上设置了gallery
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(); }}
我究竟做错了什么?
解决方法 用gallery.LayoutParams替换GrIDVIEw.LayoutParams,如下所示,这将解决您的问题imageVIEw.setLayoutParams(new gallery.LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));总结
以上是内存溢出为你收集整理的android – ClassCastException:AbsListView $LayoutParams无法强制转换为Gallery $LayoutParams全部内容,希望文章能够帮你解决android – ClassCastException:AbsListView $LayoutParams无法强制转换为Gallery $LayoutParams所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)