0
当我尝试添加一些Linearlayouts
其中每一个包含TextView
和ImageView
在运行。的OutOfMemoryError设置ImageView的
一切正常,但应用程序尝试setImageResource
时崩溃。 (的OutOfMemoryError)
我的继承人活动(相关部分):
my_icon
是在res文件夹中的可绘制的名字(名字是在每一个循环(名字来源于XML)不同
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String my_icon = parser.getValue(e, "my_icon"); // my_icon child value
int my_count = Integer.parseInt(parser.getValue(e, "my_count")); // my_count child value
String my_text = parser.getValue(e, "my_text"); // my_text child value
//Create Child-Views
LinearLayout tempLinearLayout = new LinearLayout(AnleitungActivity.this);
linearLayout.addView(tempLinearLayout);
tempLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
ViewGroup.LayoutParams params = tempLinearLayout.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = 240;
tempLinearLayout.setBackgroundColor(ContextCompat.getColor(AnleitungActivity.this, R.color.anleitung_kachel_hintergrundfarbe));
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) tempLinearLayout.getLayoutParams();
marginParams.setMargins(0,5,0,0); //für die Margin-Werte brauchen wir extra MarginLayoutParams...
tempLinearLayout.setLayoutParams(params); //layoutParams setzen
tempLinearLayout.setLayoutParams(marginParams); //MarginLayoutParams setzen
TextView tv = new TextView(AnleitungActivity.this);
tempLinearLayout.addView(tv);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(70, ViewGroup.LayoutParams.MATCH_PARENT,1f);
tv.setLayoutParams(tvParams);
tv.setText(transformText(my_text));
tv.setTextSize(17);
tv.setPadding(5,0,0,0);
tv.setTextColor(ContextCompat.getColor(AnleitungActivity.this, R.color.anleitung_text_farbe));
ImageView imageView = new ImageView(AnleitungActivity.this);
tempLinearLayout.addView(imageView);
imageView.getLayoutParams().height = dp2px(getResources(), 60);
imageView.getLayoutParams().width = dp2px(getResources(), 60);
imageView.setImageResource(getDrawableRessorceIdByName(AnleitungActivity.this, removeFileExtensionFromString(my_icon)));
}
帮助方法
public String removeFileExtensionFromString(String str){
return str.substring(0, str.indexOf("."));
}
你可以找到这个问题在这里:http://stackoverflow.com/questions/33279223/outofmemory-while-setting-image-in-imageview –
解决...图像是太大了......只是减少尺寸。 – skm
我不确定这个方法是“getDrawableRessorceIdByName”,你可以使用这个方法来代替“Resources.getIdentifier”,或者你可能已经在使用它了。 – vsatkh