获取java.lang.IllegalStateException的原因:指定的子级已具有父级
我有一个问题: java.lang.IllegalStateException:指定的子级已具有 父母亲必须首先对子级的父级调用removeView() 这是我的代码:获取java.lang.IllegalStateException的原因:指定的子级已具有父级,java,android,Java,Android,我有一个问题: java.lang.IllegalStateException:指定的子级已具有 父母亲必须首先对子级的父级调用removeView() 这是我的代码: public void fillFormules(List<Category> objectsTextToShow) { LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.layoutItemDetai
public void fillFormules(List<Category> objectsTextToShow)
{
LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.layoutItemDetail);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.mygallery);
linearLayout.removeAllViews();
for (int j = 0; j <objectsTextToShow.size() ; j++) {
ScrollView scrollView = new ScrollView(getActivity());
scrollView.setBackgroundColor(android.R.color.transparent);
scrollView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
for (int i = 0; i < objectsTextToShow.get(j).getItems().size(); i++) {
LinearLayout separator = new LinearLayout(getActivity());
LayoutParams separatorParams = new LayoutParams(LayoutParams.MATCH_PARENT, 80);
TextView textView = new TextView(getActivity());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textView.setBackgroundColor(colorUtils.titleColor);
textView.setLayoutParams(layoutParams);
//textView.setTypeface(FONT_TITLE);
textView.setTextSize(28);
textView.setText(Check.Languages(objectsTextToShow.get(j).getItems().get(i).getName(), LANGUAGE_ID));
separator.setLayoutParams(separatorParams);
linearLayout.addView(separator);
linearLayout.addView(textView);
scrollView.addView(linearLayout);
}
layoutItemDetail.addView(scrollView);
}
// scrollView.addView(linearLayout);
}
public void fillFormules(列出objectsTextToShow)
{
LinearLayout layoutItemDetail=(LinearLayout)视图.findViewById(R.id.layoutItemDetail);
LinearLayout LinearLayout=(LinearLayout)view.findViewById(R.id.mygallery);
linearLayout.removeAllViews();
对于(int j=0;j而言,问题在于这条线
scrollView.addView(linearLayout);
要初始化linearLayout
您使用的是findViewById
,这意味着linearLayout
已经有一个父视图,违反了每个视图只能有一个父视图的限制,如果子视图已经有父视图,则不能将任何视图添加到另一个视图
在您的情况下,您应该以编程方式创建LinearLayout
,就像您创建ScrollView
这样做
public void fillFormules(List<Category> objectsTextToShow)
{
LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.layoutItemDetail);
LinearLayout linearLayout;
for (int j = 0; j <objectsTextToShow.size() ; j++) {
linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL); // or VERTICAL as per your needs
ScrollView scrollView = new ScrollView(getActivity());
...
...
...
}
public void fillFormules(列出objectsTextToShow)
{
LinearLayout layoutItemDetail=(LinearLayout)视图.findViewById(R.id.layoutItemDetail);
线性布局线性布局;
对于(int j=0;j),我添加了linearLayout.removeAllViews();,因此我所做的不正确,对吗?不是。父项是layoutItemDetail
,尽管它毫无意义。如果你可能想重新思考你在那里做的事情,谢谢你救了我的命。