添加自定义视图在一个循环中引起内存不足的错误 [英] Adding custom view in a loop cause out of memory error

查看:245
本文介绍了添加自定义视图在一个循环中引起内存不足的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图多次从XML添加自定义视图到另一个视图在Android的活动。但它会导致内存不足的错误打开和关闭该活动后几次。这里是code:

I am trying to add a custom view multiple times from xml to another view in an android activity. But it causes out of memory error after opening and closing that activity few times. Here is the code:

ImageLoader imageLoader = new ImageLoader(currentActivity.getApplicationContext());
FlowLayout postsLayout = (FlowLayout) findViewById(R.id.tab_posts);
postsLayout.removeAllViews();

for (final Post post : postsData) {



                RelativeLayout profilePostItem = (RelativeLayout) View.inflate(this, R.layout.drawer_item, null);
                ImageView postPic = (ImageView) profilePostItem.findViewById(R.id.post_pic);

                String picUrl = post.getSingleImageURL();

                if(picUrl != null && !picUrl.equals("null"))
                {
                    postPic.setTag(picUrl);
                    imageLoader.DisplayImage(picUrl, currentActivity, postPic, R.drawable.default_item);

                }



                profilePostItem.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(currentActivity, PostDetailsActivity.class);
                        intent.putExtra("postID", Integer.toString(post.getPostId()));
                        currentActivity.startActivity(intent);
                    }
                });

                postsLayout.addView(profilePostItem);

            }

和这里是drawer_item.xml

and here is the drawer_item.xml

<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"              
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                >


<ImageView 
        android:id="@+id/post_pic"
        android:layout_width="140dp"
        android:layout_height="140dp"       
        android:background="#FFFFFF" 
        android:src="@drawable/default_user_pic"
    />   


</RelativeLayout>

下面是LogCat中输出:

Here is the LogCat output:

    09-06 22:32:45.566: D/dalvikvm(11857): GC_BEFORE_OOM freed <1K, 6% free 46549K/49095K, paused 67ms, total 67ms
09-06 22:32:45.566: E/dalvikvm-heap(11857): Out of memory on a 67816-byte allocation.
09-06 22:32:45.566: I/dalvikvm(11857): "Thread-15875" prio=4 tid=53 RUNNABLE
09-06 22:32:45.566: I/dalvikvm(11857):   | group="main" sCount=0 dsCount=0 obj=0x4325e6d8 self=0x53269790
09-06 22:32:45.566: I/dalvikvm(11857):   | sysTid=12239 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1395039200
09-06 22:32:45.566: I/dalvikvm(11857):   | schedstat=( 124808833 28013003 46 ) utm=10 stm=1 core=1
09-06 22:32:45.566: I/dalvikvm(11857):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-06 22:32:45.566: I/dalvikvm(11857):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
09-06 22:32:45.566: I/dalvikvm(11857):   at com.company.testapp.utils.ImageLoader.decodeFile(ImageLoader.java:139)
09-06 22:32:45.566: I/dalvikvm(11857):   at com.company.testapp.utils.ImageLoader.getBitmap(ImageLoader.java:81)
09-06 22:32:45.566: I/dalvikvm(11857):   at com.company.testapp.utils.ImageLoader.access$0(ImageLoader.java:73)
09-06 22:32:45.566: I/dalvikvm(11857):   at com.company.testapp.utils.ImageLoader$PhotosLoader.run(ImageLoader.java:196)
09-06 22:32:45.566: D/skia(11857): --- decoder->decode returned false
09-06 22:32:45.566: W/dalvikvm(11857): threadid=53: thread exiting with uncaught exception (group=0x41aee2a0)
09-06 22:32:45.566: E/AndroidRuntime(11857): FATAL EXCEPTION: Thread-15875
09-06 22:32:45.566: E/AndroidRuntime(11857): java.lang.OutOfMemoryError
09-06 22:32:45.566: E/AndroidRuntime(11857):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-06 22:32:45.566: E/AndroidRuntime(11857):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
09-06 22:32:45.566: E/AndroidRuntime(11857):    at com.company.testapp.utils.ImageLoader.decodeFile(ImageLoader.java:139)
09-06 22:32:45.566: E/AndroidRuntime(11857):    at com.company.testapp.utils.ImageLoader.getBitmap(ImageLoader.java:81)
09-06 22:32:45.566: E/AndroidRuntime(11857):    at com.company.testapp.utils.ImageLoader.access$0(ImageLoader.java:73)
09-06 22:32:45.566: E/AndroidRuntime(11857):    at com.company.testapp.utils.ImageLoader$PhotosLoader.run(ImageLoader.java:196)
09-06 22:32:45.651: I/dalvikvm-heap(11857): Clamp target GC heap from 48.757MB to 48.000MB
09-06 22:32:45.651: D/dalvikvm(11857): GC_FOR_ALLOC freed 180K, 6% free 46583K/49159K, paused 68ms, total 68ms
09-06 22:32:45.651: I/dalvikvm-heap(11857): Forcing collection of SoftReferences for 63616-byte allocation
09-06 22:32:45.736: I/dalvikvm-heap(11857): Clamp target GC heap from 48.757MB to 48.000MB
09-06 22:32:45.736: D/dalvikvm(11857): GC_BEFORE_OOM freed 0K, 6% free 46583K/49159K, paused 82ms, total 82ms
09-06 22:32:45.736: E/dalvikvm-heap(11857): Out of memory on a 63616-byte allocation.
09-06 22:32:45.736: I/dalvikvm(11857): "Thread-15874" prio=4 tid=49 RUNNABLE

谁能告诉为什么它正在泄漏内存?

Can anyone tell why it is leaking memory?

推荐答案

我用的通用图像装载机

这篇关于添加自定义视图在一个循环中引起内存不足的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆