我使用以这种方式实现ListView
方法的ArrayAdapter
创建了getView()
:
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView ==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.rowlayout_member, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.label);
TextView textView1 = (TextView) rowView.findViewById(R.id.label1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
textView1.setText(surname[position]);
String s = values[position];
if(foto[position] != null){
Drawable c = Drawable.createFromPath(basePath+foto[position]); // line 56
if(c!=null)
imageView.setImageDrawable(c);
else
imageView.setImageResource(R.drawable.default);
}else{
imageView.setImageResource(R.drawable.default);
}
return rowView;
}
所有似乎都运行良好,但当我进入列表多次(更改活动)时,有时我会收到以下错误:
08-06 15:06:14.110: E/dalvikvm-heap(3527): Out of memory on a 2380816-byte allocation.
08-06 15:06:14.110: W/dalvikvm(3527): threadid=1: thread exiting with uncaught exception (group=0x40a3d1f8)
08-06 15:06:14.120: E/AndroidRuntime(3527): FATAL EXCEPTION: main
08-06 15:06:14.120: E/AndroidRuntime(3527): java.lang.OutOfMemoryError
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:880)
08-06 15:06:14.120: E/AndroidRuntime(3527): at host.framework.component.MySimpleArrayAdapter.getView(MySimpleArrayAdapter.java:56)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.AbsListView.obtainView(AbsListView.java:2012)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.ListView.makeAndAddView(ListView.java:1772)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.ListView.fillUp(ListView.java:705)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.ListView.fillGap(ListView.java:645)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3813)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.os.Handler.handleCallback(Handler.java:605)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.os.Looper.loop(Looper.java:137)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-06 15:06:14.120: E/AndroidRuntime(3527): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 15:06:14.120: E/AndroidRuntime(3527): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 15:06:14.120: E/AndroidRuntime(3527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-06 15:06:14.120: E/AndroidRuntime(3527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-06 15:06:14.120: E/AndroidRuntime(3527): at dalvik.system.NativeStart.main(Native Method)
08-06 15:06:14.120: W/ActivityManager(149): Force finishing activity host.activity/.ACT_CircoliDiSupporto
08-06 15:06:14.640: W/ActivityManager(149): Activity pause timeout for ActivityRecord{413de508 host.activity/.ACT_CircoliDiSupporto}
08-06 15:06:24.140: W/ActivityManager(149): Launch timeout has expired, giving up wake lock!
08-06 15:06:24.650: W/ActivityManager(149): Activity idle timeout for ActivityRecord{41389d58 host.activity/.HostActivity}
第56行(代码中突出显示)似乎生成了错误
Drawable c = Drawable.createFromPath(basePath+foto[position]);
答案 0 :(得分:3)
目前的实施存在许多问题:
Drawable c = Drawable.createFromPath(basePath+foto[position]); // line 56
)上构建一个Bitmap,这会使您的列表滚动相当不稳定我建议您使用Universal Image Loader库(https://github.com/nostra13/Android-Universal-Image-Loader)来解决上述所有问题。虽然我不确定它是否提供本机本地图像加载功能,或者您是否必须扩展某些类才能具备此类功能。
<强> [编辑] 强>
我不建议推出自己的解决方案,因为它会花费比你想象的更多的时间(相信我......)。但如果您真的想这样做,可以查看本教程:http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-images/
上述教程解决了#1&amp; #2,对于#3,请检查:Strange out of memory issue while loading an image to a Bitmap object
答案 1 :(得分:1)
首先,您需要一个像LruCache这样的数据结构来保存正在创建的drawable。
如何确定我的物品是否正确释放?
在活动中使用数据结构销毁,确保使类的对象为空或清除它的内部对象。 (这将告诉gc该对象已准备好被清除(在大多数情况下))。
同样在主线程上运行文件操作不是最好的方法,因为它会在listview滚动中引入急动。尝试在辅助线程中执行此操作。