与动态地添加的组件冻结活动 [英] Activity with dynamicly added components freezes
问题描述
我的应用程序冻结和LogCat中成长堆(frag的情况下),以添加,如果更多然后3自己的成分,以目前的活动/ 14MB布局。直到第四次组件被加入这需要很长的和Android要求关闭我的申请。
我已经写了其中有一些组件(EditTexts)和自己的成分一活动。我想要做类似接触应用(数字增加),但成分的东西。所以,我创建了一个类IngredientsLayout延伸的LinearLayout和一个TextView和添加 - 按钮我已经放置在活动-布局布局xml文件。我每次preSS Add按钮,一个IngredientLayout 3 EditTexts和的ImageButton将被添加到当前IngredientsLayout应该持有的所有添加成分。
我在做什么错了?
IngredientsLayout类:
公共类IngredientsLayout扩展的LinearLayout {的ImageButton imageButtonAddIngredient;
ArrayList的< IngredientLayout> ArrayList的;公共IngredientsLayout(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
LayoutInflater吹气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ingredients_list,这一点); ArrayList的=新的ArrayList< IngredientLayout>();
imageButtonAddIngredient =(的ImageButton)findViewById(R.id.imageButtonAddIngredient);
imageButtonAddIngredient.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
的LinearLayout布局=(的LinearLayout)findViewById(R.id.linearLayoutIngredientsList);
IngredientLayout ingredientLayout =新IngredientLayout(的getContext(),NULL);
ingredientLayout.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
arrayList.add(ingredientLayout);
layout.addView(ingredientLayout);
} });
}
}
ingredients_layout.xml:
<?XML版本=1.0编码=UTF-8&GT?;
<合并的xmlns:机器人=http://schemas.android.com/apk/res/android>
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / linearLayoutIngredientsList
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:方向=横向>
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_gravity =左| center_vertical
机器人:layout_weight =1
机器人:文字=@字符串/ ingredients_list_name/>
<的ImageButton
机器人:ID =@ + ID / imageButtonAddIngredient
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@机器人:彩色/透明
机器人:SRC =@绘制/ ic_menu_add_ingredient/>
< / LinearLayout中>
< / LinearLayout中>
< /合并>
IngredientLayout类:
公共类IngredientLayout扩展的LinearLayout {
公共IngredientLayout(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ingredients_list_row,这一点);
}
}
ingredients_list_row.xml
<?XML版本=1.0编码=UTF-8&GT?;
<合并的xmlns:机器人=http://schemas.android.com/apk/res/android>
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / linearLayoutIngredientRow
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <的EditText
机器人:ID =@ + ID / editTextAmount
机器人:layout_width =45dp
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.11
机器人:EMS =10
机器人:提示=@字符串/ ingredients_list_listrow_amount
安卓的inputType =numberDecimal> < requestFocus的/>
< /&的EditText GT; <的EditText
机器人:ID =@ + ID / editTextUnit
机器人:layout_width =60dp
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.06
机器人:EMS =10
机器人:提示=@字符串/ ingredients_list_listrow_unit/><的EditText
机器人:ID =@ + ID / editTextName
机器人:layout_width =127dp
机器人:layout_height =match_parent
机器人:layout_gravity =左
机器人:layout_weight =0.005
机器人:EMS =10
机器人:提示=@字符串/ ingredients_list_listrow_name/><的ImageButton
机器人:ID =@ + ID / imageButton1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@机器人:彩色/透明
机器人:SRC =@绘制/ ic_menu_cancel/>< / LinearLayout中>
< /合并>
LinearLayouts并不意味着这种动态访问的。
你需要做的是一个ListView和一个适配器。
和每次添加一个新的项目到你调用它的NottifyDataChanged更新新项目的列表视图中的适配器的时间。
My Application freezes and LogCat grows heap (frag case) to 14mb if add more then 3 own "components" to current activity/layout. It takes very long until the fourth "component" is added and Android asks to close my application.
I've written a Activity which has some components (EditTexts) and a own "component". I want to do something similar to the contacts app (Number adding) but with ingredients. So I've created a class IngredientsLayout which extends LinearLayout and a Layout xml file with a TextView and a "Add"-Button which I've placed on the Activity-Layout. Every time I press the Add Button, a single IngredientLayout with 3 EditTexts and a ImageButton will be added to the current IngredientsLayout which should "hold" all added ingredients.
What I'm doing wrong?
IngredientsLayout class:
public class IngredientsLayout extends LinearLayout {
ImageButton imageButtonAddIngredient;
ArrayList<IngredientLayout> arrayList;
public IngredientsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ingredients_list, this);
arrayList = new ArrayList<IngredientLayout>();
imageButtonAddIngredient = (ImageButton) findViewById(R.id.imageButtonAddIngredient);
imageButtonAddIngredient.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutIngredientsList);
IngredientLayout ingredientLayout = new IngredientLayout(getContext(), null);
ingredientLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
arrayList.add(ingredientLayout);
layout.addView(ingredientLayout);
}
});
}
}
ingredients_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutIngredientsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:text="@string/ingredients_list_name" />
<ImageButton
android:id="@+id/imageButtonAddIngredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/ic_menu_add_ingredient" />
</LinearLayout>
</LinearLayout>
</merge>
IngredientLayout class:
public class IngredientLayout extends LinearLayout {
public IngredientLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ingredients_list_row, this);
}
}
ingredients_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutIngredientRow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextAmount"
android:layout_width="45dp"
android:layout_height="fill_parent"
android:layout_weight="0.11"
android:ems="10"
android:hint="@string/ingredients_list_listrow_amount"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editTextUnit"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="0.06"
android:ems="10"
android:hint="@string/ingredients_list_listrow_unit" /><EditText
android:id="@+id/editTextName"
android:layout_width="127dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="0.005"
android:ems="10"
android:hint="@string/ingredients_list_listrow_name" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/ic_menu_cancel" />
</LinearLayout>
</merge>
LinearLayouts are not meant for this kind of dynamic accessing. What you need to do is a ListView and a adapter.
And every time you add a new item into the adapter you call it's NottifyDataChanged to update the list view with the new item.
这篇关于与动态地添加的组件冻结活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!