gpt4 book ai didi

java - 多次滚动时出现 OutOfMemoryException

转载 作者:行者123 更新时间:2023-11-30 04:11:28 26 4
gpt4 key购买 nike

我在 gridview 滚动中遇到问题。我制作了一个自定义 gridview 并在原始文件中插入小部件并通过 View 对其进行膨胀。我得到了正确的结果和正确的数据,一切都很顺利,但是当我快速向上滚动 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 GridViewAdapter 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 GridViewAdapter(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.gridviewrow, 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 有问题。

最佳答案

在 getView() 中,您不是重复使用 convertView,而是总是用充气器给新的充气。这需要更多内存并使您的代码变慢。此外,请检查您是否没有使用 imageLoader 泄漏图像。

关于java - 多次滚动时出现 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com