我有这个问题:


  java.lang.IllegalStateException:指定的子对象已经有一个
  父母您必须先在孩子的父母上调用removeView()。


这是我的代码:

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);

    }

最佳答案

问题是这条线

scrollView.addView(linearLayout);


初始化linearLayout您正在使用findViewById,这意味着linearLayout已经有一个父级,这违反了每个视图只能有一个父级的约束

08-18 17:59