我正在开发一个 Android 应用程序。活动为每一行加载两个位图。由于内存不足,应用程序在上下滚动几次后崩溃
例外是
01-04 12:58:45.328: E/dalvikvm-heap(30264): Out of memory on a 307216-byte allocation.
01-04 12:58:45.328: I/dalvikvm(30264): "main" prio=5 tid=1 RUNNABLE
01-04 12:58:45.328: I/dalvikvm(30264): | group="main" sCount=0 dsCount=0 obj=0x40c40460 self=0xb3ca38
01-04 12:58:45.328: I/dalvikvm(30264): | sysTid=30264 nice=0 sched=0/0 cgrp=default handle=1074541800
01-04 12:58:45.328: I/dalvikvm(30264): | schedstat=( 14267042000 1117190000 14168 ) utm=1325 stm=101 core=1
01-04 12:58:45.328: I/dalvikvm(30264): at android.graphics.Bitmap.nativeCreate(Native Method)
这是我的主要活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/DefaultMargin"
android:paddingTop="@dimen/DefaultMargin" >
<TextView
android:id="@+id/textViewCourseEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_course_list_label"
android:visibility="gone" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
ListView 项目如下所示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/DefaultMargin"
android:layout_weight="0.65" >
<ImageView
android:id="@+id/coursestatelogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/DefaultMargin"
android:layout_weight="0.2"
android:gravity="right" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:gravity="right" >
<TextView
android:id="@+id/courselabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/HalfMargin"
android:paddingRight="@dimen/HalfMargin"
android:singleLine="false"
android:textSize="@dimen/textSize" />
</LinearLayout>
<ImageView
android:id="@+id/courselogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
阵列适配器代码:
public class CourseArrayAdapter extends ArrayAdapter<CourseData>{
private final Context context;
private final CourseData[] values;
public CourseArrayAdapter(Context context, CourseData[] values) {
super(context, R.layout.list_course_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_course_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.courselabel);
textView.setText(Farsi.Convert(values[position].Name));
ImageView imageView = (ImageView) rowView.findViewById(R.id.courselogo);
ImageView imageViewstate = (ImageView) rowView.findViewById(R.id.coursestatelogo);
switch (values[position].State){
case NotStarted :
if (values[position].Downloaded)
imageViewstate.setImageResource(R.drawable.downloaded);
else
imageViewstate.setImageResource(R.drawable.download);
break;
case InProgress :
imageViewstate.setImageResource(R.drawable.lesson);
break;
case Certified :
imageViewstate.setImageResource(R.drawable.certificate);
break;
}
return rowView;
}
}