gpt4 book ai didi

android - 带有图像的滚动列表上的 Android 应用程序内存不足

转载 作者:行者123 更新时间:2023-11-29 16:10:07 25 4
gpt4 key购买 nike

我正在开发 Android 应用程序。 Activity 为每一行加载两个位图。由于 Out Of Memory

,应用程序在几次上下滚动后崩溃

异常(exception)是

   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)

这是我的主要 Activity

<?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;
}

最佳答案

您应该检查 contvertView 是否为 null。

View rowView;
if (convertView== null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

rowView = inflater.inflate(R.layout.list_course_item, parent, false);
}else
rowView = convertView;

关于android - 带有图像的滚动列表上的 Android 应用程序内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com