膨胀抛出OutOfResourcesException

时间:2015-01-20 00:50:31

标签: android listview adapter layout-inflater

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

1 个答案:

答案 0 :(得分:1)

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

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

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