[英]Memory leak in dynamically creating linearlayout and textviews
I am having problem creating lots of LinearLayout
and TextView
basically my app displays text and I dynamically create rows( LinearLayout
) and words( TexView
) in the corresponding row. 我在创建大量LinearLayout
和TextView
时遇到问题,基本上我的应用程序显示文本,并且在相应的行中动态创建行( LinearLayout
)和单词( TexView
)。 Now my app also uses FragmentStatePagerAdapter
so I can "change the page". 现在,我的应用程序还使用FragmentStatePagerAdapter
因此我可以“更改页面”。 I have no problem with that. 对于那件事我没有任何疑问。 The problem is when I redirect to my own activity with a new textsize it somehow leaks when adding rows. 问题是当我使用新的textsize重定向到自己的活动时,添加行时以某种方式泄漏。
int threshold = 25;
int tw = 0;
LinearLayout ll = rootView.findViewById(R.id.container);
LinearLayout row = new LinearLayout(a);
for(int x = 0;x<words.size();x++)
{
tw+=words.get(x).length;
TextView tv = new TextView(a);
tv.setText(words.get(x));
if(tw > threshold)
{
ll.addView(row) <----- this is where out of memory points me
row = new LinearLayout(a);
tw = 0;
x-=1;
}
else
row.addView(tv);
}
Now I also tried dumping the heap and analyzing with mat but the dump I am creating has an invalid header error so I don't know where the actual leak comes from other than the actual error log I have in logcat that points to the ll adding rows which I don't know if is the right place to look into. 现在我也尝试转储堆并使用mat进行分析,但是我正在创建的转储具有无效的标头错误,因此我不知道实际的泄漏来自哪里,除了我在logcat中指向ll添加的实际错误日志之外我不知道是否适合查找的行。
Edited : Some added info. 编辑:添加了一些信息。 It seems the problem occurs when I am adjusting the row height through linearlayout params because when i set my own default which is 50 it is usually just fine. 当我通过linearlayout参数调整行高时,似乎会出现问题,因为当我将自己的默认值设置为50时,通常就可以了。 Anyone one knows the reason for this? 有人知道原因吗?
Your loop may end into a dead loop for some conditions. 在某些情况下,您的循环可能会陷入死循环。
For instance,if your words
list contains a String
which length is over threshold
(25),the if statement( tw > threshold
) would forever evaluate to true,and because you also minus loop count x
by one,this is where dead loop happens. 例如,如果您的words
列表中包含一个长度超过threshold
(25)的String
,则if语句( tw > threshold
)将永远评估为true,并且由于您还将循环计数x
减一,因此发生死循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.