膨胀抛出 OutOfResourcesException

inflate throwing OutOfResourcesException

我正在尝试使用自定义适配器创建一个列表,但是在我调用 inflate 的代码部分,我得到了 OutOfResourcesException,我进行了几次测试,但我没有发现原因

当调试执行这一行时:rowView = inflater.inflate(R.layout.list_chat, parent, false); 然后调用异常。

public class ListChatAdapter extends BaseAdapter{
    public class Contact {
        private String name;
        private String sentence;
        private Bitmap img;

        Contact(String name, String sentence, Bitmap bmp) {
            this.name = name;
            this.sentence = sentence;
            this.img = bmp;
        }

        void setName(String name) {
            this.name = name;
        }

        void setSentence(String sentence) {
            this.sentence = sentence;
        }

        void setBmp(Bitmap bmp) {
            this.img = bmp;
        }

        String getName() {
            return this.name;
        }

        String getSentence() {
            return this.sentence;
        }

        Bitmap getImg() {
            return this.img;
        }
    }

    private static class ViewHolderItem {
        TextView textName;
        TextView textSentence;
        ImageView img;
    }

    private static int count = 0;
    private static List<Contact> contacts = new ArrayList<Contact>();

    private Context context;

    public ListChatAdapter(Context c) {
        context = c;
    }

    @Override
    public int getCount() {
        return count;
    }

    public void addItem(Contact contact) {
        contacts.add(contact);
        count++;
        notifyDataSetChanged();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderItem viewHolder;
        View rowView = convertView;

        if(rowView==null){

            // inflate the layout
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            // Exception here
            rowView = inflater.inflate(R.layout.list_chat, parent, false);

            // well set up the ViewHolder
            viewHolder = new ViewHolderItem();
            viewHolder.textName = (TextView) convertView
                    .findViewById(R.id.text_name);

            viewHolder.textSentence = (TextView) convertView
                    .findViewById(R.id.text_sentence);

            viewHolder.img = (ImageView) convertView.findViewById(R.id.img_chat);

            // store the holder with the view.
            rowView.setTag(viewHolder);

        }else{
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (ViewHolderItem) rowView.getTag();
        }

        // object item based on the position
        Contact objectItem = contacts.get(position);

        // assign values if the object is not null
        if(objectItem != null) {
            // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
            viewHolder.textName.setText(objectItem.getName());
            viewHolder.textSentence.setText(objectItem.getSentence());
            viewHolder.img.setImageBitmap(objectItem.getImg());
        }

        return rowView;
    }

}

布局调用list_chat.xml

<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/vlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation = "vertical" >
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="4dp"
    android:paddingRight="4dp" >

    <ImageView 
        android:id="@+id/img_chat"
        android:layout_width="55dp"
        android:layout_height="65dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView 
        android:id="@+id/text_name"    
        android:layout_alignRight="@id/img_chat"
        android:layout_alignEnd="@id/img_chat"
        android:layout_alignParentTop="true" />

    <TextView 
        android:id="@+id/text_sentence"    
        android:layout_alignRight="@id/img_chat"
        android:layout_alignEnd="@id/img_chat"
        android:layout_below="@id/text_sentence" />

</RelativeLayout>
</Linearlayout>

检查传入图像的大小。即使您的视图尺寸较小,默认情况下也会加载整个图像然后进行缩放。因此,如果您正在加载非常大的图像,您可以快速耗尽 VM 的堆。

要注意的另一件事是您对 RelativeLayout 的使用和子视图的对齐方式。最后一个TextViewtext_sentence的id有android:layout_below="@id/text_sentence",所以它告诉系统将它对齐到自己下面。我将是 Eclipse 或 Android Studio(无论您使用的是哪个)将此标记为问题。所以 inflation 上发生的事情是布局正在膨胀,但 RelativeLayout 正在递归地尝试测量和布局这个子视图并耗尽资源。您很可能打算将此对齐方式设置为低于 @id/text_name.

另外请注意,现在父级 LinearLayout 没有为您做任何事情,因此您可以通过删除它来消除额外的开销。这对于 1 的列表不会产生太大影响,但是当您开始在列表中获得许多项目时,这将对性能产生负面影响。只要有可能,就应该从布局的视图层次结构中删除不必要的视图。