滚动很多次时OutOfMemoryException

时间:2012-05-29 03:28:16

标签: java android

我在gridview滚动中遇到了问题。我制作了一个自定义gridview并在原始文件中插入小部件并通过视图对它们进行充气。我得到了正确的结果和正确的数据,一切都很顺利但是当我快速向下滚动gridview 3-4次时,它会提升OutOfMemoryException

此应用包含已安装的应用列表和图标

的列表

这是我的客户适配器代码:

  package com.AppFavorits;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.AppFavorits.ImageLoad.ImageLoader;

public class GriedViewAdapter extends BaseAdapter {
    private Context activit;
    LayoutInflater inflator;
    public RelativeLayout imgvGridItem;
    public TextView txtGridItemlabel;
    public CheckBox chkbxGridItem;
     ArrayList<PInfo> lstpinfo = new ArrayList<PInfo>();
     public ImageLoader imageLoader; 
     public GriedViewAdapter(Context m1, ArrayList<PInfo> lstpinfo) {

            activit = m1;
            inflator = LayoutInflater.from(m1);
            this.lstpinfo = lstpinfo;
              imageLoader=new ImageLoader(m1.getApplicationContext());

        }

    public static class ViewHolder {

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        // LayoutInflater inflator = activit.getLayoutInflater();


        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.griedviewrow, null);

        imgvGridItem = (RelativeLayout) convertView
                .findViewById(R.id.rlGreidItemicon);
        txtGridItemlabel = (TextView) convertView
                .findViewById(R.id.txtGridItemlabel);
        chkbxGridItem = (CheckBox) convertView
                .findViewWithTag(R.id.chkbxGridItem);
        if ((lstpinfo.get(position).appname.toString()) != null){
            Drawable d = lstpinfo.get(position).icon;  
            //new ImageLoad().execute();

            imgvGridItem.addView(getimageviewimage(lstpinfo.get(position).icon));
            txtGridItemlabel.setText(lstpinfo.get(position).appname.toString());

            // convertView.setTag(view);
        }

        return convertView;

    }

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

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

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

    public RelativeLayout getimageviewimage(Drawable d) {
        RelativeLayout seprator = new RelativeLayout(activit);
        seprator.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        seprator.setGravity(Gravity.LEFT | Gravity.CENTER);

        ImageView imgmap = new ImageView(activit);
        imgmap.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        // imgmap.setBackgroundResource(R.drawable.a13);
        Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
        imageLoader.DisplayImage(bitmap, imgmap);

        //imgmap.setImageBitmap(bitmap);
        seprator.setPadding(5, 5, 15, 5);
        seprator.addView(imgmap);

        return seprator;

    }
     private class ImageLoad extends AsyncTask<Void, Void, Void> {
           // private final ProgressDialog dialog = new ProgressDialog(tranning.this);
            protected void onPreExecute() {
            //  this.dialog.setMessage("Please Wait...");
             // this.dialog.show();
              // put your code which preload with processDialog  
            }
              @Override
              protected Void doInBackground(Void... arg0) {
                   // put your code here
                   return null;
              }

             @Override
             protected void onPostExecute(final Void unused) {
                /*if (this.dialog.isShowing()) {
                this.dialog.dismiss();
             }   */
          }
      }
}

我认为getview存在问题。

3 个答案:

答案 0 :(得分:1)

在getView()中,您不会重复使用convertView,而是始终使用inflator来充气。这需要更多内存并使代码更慢。另外,请检查您是否没有使用imageLoader泄漏图像。

答案 1 :(得分:0)

out of memory通常意味着没有足够的内存来加载文件。尝试关闭其他所有内容(或至少其他一些应用程序)并再次尝试。

答案 2 :(得分:0)

下载代码来自以下链接并使用gridview而不是listview

Lazy Loading Example