【问题标题】:inflate throwing OutOfResourcesException膨胀抛出 OutOfResourcesException
【发布时间】:2015-01-20 00:50:31
【问题描述】:

我正在尝试使用自定义适配器做一个列表,但是在我调用 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>

【问题讨论】:

    标签: android listview adapter layout-inflater


    【解决方案1】:

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

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

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

    【讨论】:

    • 但是我用于测试的这张图片是应用程序的默认图标,我在其他示例中使用了这张图片,没有充气,我没有遇到问题。
    • 图片的分辨率是多少?
    • 提供带有堆栈跟踪的 logcat 输出也会很有帮助。您的列表中有多少条目?
    • 即使没有图像,我在同一行也有问题,我只有一个条目。
    • 我想我看到了这个问题。将更新我上面的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2016-09-20
    • 2016-07-02
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多