linearlayout 以编程方式导致错误 "The specified child already has a parent. You must call removeView() on the child's parent first."
linearlayout programmatically causes error "The specified child already has a parent. You must call removeView() on the child's parent first."
我有一个 xml 格式的现有片段
<LinearLayout
android:id="@+id/phoneLinearLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="@dimen/title_mar_gap"
android:paddingRight="10dp"
android:paddingTop="@dimen/title_mar_gap"
android:orientation="vertical">
<TextView
android:id="@+id/CL"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|left|center_horizontal"
android:text="Contact No"
android:textSize="@dimen/text_small" />
<TextView
android:id="@+id/CNV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:paddingBottom="5dp"
android:textColor="@color/black"
android:text=""
android:textSize="@dimen/text_medium" />
<Button
android:id="@+id/callButton"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/white"
android:background="@color/Blue"
android:text="Call" />
</LinearLayout>
现在我有 activity class 在 oncreateview 中我有以下代码 -
View rootView = inflater.inflate(R.layout.fragment_above_fragmentView, container, false);
LinearLayuot llayout = (LinearLayout) rootView.findViewById(R.id.phoneLinearLyout);
我正在尝试使用以下代码在线性布局 Fragment_above_fragmentView 中动态显示 TextView(如第一个 XML 帖子中所述)。
for (int i=0;i<2(variable here);i++)
{
contactDisplay = (TextView) rootView.findViewById(R.id.contanctNumberValue);
contactDisplay.setTypeface(typeface);
contactDisplay.setText(dataModel.getTelNo());
//contactDisplay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
llayout.addView(contactDisplay);
//phoneLinearLyout.updateViewLayout(contactDisplay,);
}
但是,面临由以下原因引起的错误:java.lang.IllegalStateException:指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView(),否则应用程序会崩溃。
您只需在将视图添加到布局之前删除所有视图即可:
llayout.removeAllViews(); //add this
for (int i=0;i<2(variable here);i++)
{
contactDisplay = (TextView) rootView.findViewById(R.id.contanctNumberValue);
contactDisplay.setTypeface(typeface);
contactDisplay.setText(dataModel.getTelNo());
//contactDisplay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
llayout.addView(contactDisplay);
//phoneLinearLyout.updateViewLayout(contactDisplay,);
}
for (int i=0;i<2(此处为变量);i++)
{
contactDisplay = (TextView) rootView.findViewById(R.id.contanctNumberValue);
contactDisplay.setTypeface(typeface);
if(contactDisplay.getParent()!=null)
((ViewGroup)tv.getParent()).removeView(contactDisplay);
contactDisplay.setText(dataModel.getTelNo());
//contactDisplay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
llayout.addView(contactDisplay);
//phoneLinearLyout.updateViewLayout(contactDisplay,);
}
您应该创建一个新的 TextView
并添加到布局中。
for (int i=0;i<2(variable here);i++)
{
TextView contactDisplay = new TextView(getApplicationContext());
contactDisplay.setTypeface(typeface);
contactDisplay.setText(dataModel.getTelNo());
llayout.addView(contactDisplay);
}
每次都创建文本视图,而不是指向已使用的同一个文本视图。
我有一个 xml 格式的现有片段
<LinearLayout
android:id="@+id/phoneLinearLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="@dimen/title_mar_gap"
android:paddingRight="10dp"
android:paddingTop="@dimen/title_mar_gap"
android:orientation="vertical">
<TextView
android:id="@+id/CL"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|left|center_horizontal"
android:text="Contact No"
android:textSize="@dimen/text_small" />
<TextView
android:id="@+id/CNV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:paddingBottom="5dp"
android:textColor="@color/black"
android:text=""
android:textSize="@dimen/text_medium" />
<Button
android:id="@+id/callButton"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/white"
android:background="@color/Blue"
android:text="Call" />
</LinearLayout>
现在我有 activity class 在 oncreateview 中我有以下代码 -
View rootView = inflater.inflate(R.layout.fragment_above_fragmentView, container, false);
LinearLayuot llayout = (LinearLayout) rootView.findViewById(R.id.phoneLinearLyout);
我正在尝试使用以下代码在线性布局 Fragment_above_fragmentView 中动态显示 TextView(如第一个 XML 帖子中所述)。
for (int i=0;i<2(variable here);i++)
{
contactDisplay = (TextView) rootView.findViewById(R.id.contanctNumberValue);
contactDisplay.setTypeface(typeface);
contactDisplay.setText(dataModel.getTelNo());
//contactDisplay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
llayout.addView(contactDisplay);
//phoneLinearLyout.updateViewLayout(contactDisplay,);
}
但是,面临由以下原因引起的错误:java.lang.IllegalStateException:指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView(),否则应用程序会崩溃。
您只需在将视图添加到布局之前删除所有视图即可:
llayout.removeAllViews(); //add this
for (int i=0;i<2(variable here);i++)
{
contactDisplay = (TextView) rootView.findViewById(R.id.contanctNumberValue);
contactDisplay.setTypeface(typeface);
contactDisplay.setText(dataModel.getTelNo());
//contactDisplay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
llayout.addView(contactDisplay);
//phoneLinearLyout.updateViewLayout(contactDisplay,);
}
for (int i=0;i<2(此处为变量);i++) {
contactDisplay = (TextView) rootView.findViewById(R.id.contanctNumberValue);
contactDisplay.setTypeface(typeface);
if(contactDisplay.getParent()!=null) ((ViewGroup)tv.getParent()).removeView(contactDisplay);
contactDisplay.setText(dataModel.getTelNo());
//contactDisplay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
llayout.addView(contactDisplay);
//phoneLinearLyout.updateViewLayout(contactDisplay,);
}
您应该创建一个新的 TextView
并添加到布局中。
for (int i=0;i<2(variable here);i++)
{
TextView contactDisplay = new TextView(getApplicationContext());
contactDisplay.setTypeface(typeface);
contactDisplay.setText(dataModel.getTelNo());
llayout.addView(contactDisplay);
}
每次都创建文本视图,而不是指向已使用的同一个文本视图。