android - 滚动图像列表时出现 OutOfMemory 异常

标签 android listview out-of-memory

我有一个包含 70 个带有图像图标的文本项的列表(存储在 drawables 文件夹中)。 如果我第一次启动应用程序并缓慢滚动列表 - 不会发生异常。

当第一次启动应用程序并且我使用“fling”操作滚动列表时,发生以下异常:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:439)
 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:322)
 at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:688)
 at android.content.res.Resources.loadDrawable(Resources.java:1710)
 at android.content.res.Resources.getDrawable(Resources.java:585)

之后,如果我使用 DDMS 终止应用程序,再次启动它并使用“fling”操作滚动它,则不会发生异常。因此只有在第一次安装和启动应用程序时才会出现异常。

这是我用于列表的适配器:

public class AuthorAdapter extends ArrayAdapter<Author> {

private ArrayList<Author> items;
private Context context;
private LayoutInflater inflater;

public AuthorAdapter(Context context, int textViewResourceId, ArrayList<Author> items) {
    super(context, textViewResourceId, items);
    this.items = items;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}

static class ViewHolder {
    ImageView authorFace;
    TextView authorName;
    TextView authorWho;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.autor_data, null);

        holder = new ViewHolder();
        holder.authorFace = (ImageView) convertView.findViewById(R.id.authorFaceImageView);
        holder.authorName = (TextView) convertView.findViewById(R.id.authorNameTextView);
        holder.authorWho = (TextView) convertView.findViewById(R.id.authorWhoTextView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Author author = items.get(position);
    if (author != null) {
        if (holder.authorFace != null) {
            String faceFileName = author.getFace();
            String facePlaceName = context.getPackageName() + ":drawable/" + faceFileName.subSequence(0, faceFileName.lastIndexOf("."));
            int faceFileId = context.getResources().getIdentifier(facePlaceName, null, null);
            holder.authorFace.setImageResource(faceFileId);
        }
        if (holder.authorName != null) {
            holder.authorName.setText(author.getName());
        }
        if (holder.authorWho != null) {
            holder.authorWho.setText(author.getWho());
        }
    }
    return convertView;
}

有没有办法避免异常? 我应该捕获异常并以某种方式逃避内存吗?

有什么建议可以实现吗?

谢谢。

最佳答案

您无法捕获 java.lang.OutOfMemoryError。它们几乎是致命的。

现在,您说问题只在您第一次运行应用程序时出现。这让我认为问题与 View 代码有关。如果用户抛出一个资源繁重的 View ,它可能会使用大量内存,但正如您所说,通常您的应用程序运行良好。

那么 - 第一次运行您的应用程序时,您会进行哪些初始化?是否有可能是第一次运行一次的任务之一正在泄漏内存,这意味着没有足够的剩余空间来使用您的 View ?

关于android - 滚动图像列表时出现 OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2070412/

相关文章:

android - SQLite 创建具有唯一列组合的表

android - Adb kill-server 没有响应?

c# - ListView 列是否有可显示的最大长度?

c# - 以编程方式在 Asp.Net ListView 中选择项目

java - 膨胀 ListView 二进制 XML 行 0 时出错#

java - 线程中出现异常 "main"java.lang.OutOfMemoryError : Java heap space while using util Packages

java - Netbeans 模块开发内存不足

android - 火力地堡安卓 : Retrieve post from current user's friends only

android - 无法激活 java 类型 MvxRecyclerView 的 JNI 句柄

java,内存不足错误: Java heap space