【问题标题】:why I get java.lang.IllegalStateException: The specified child already has a parent为什么我得到 java.lang.IllegalStateException:指定的孩子已经有一个父母
【发布时间】:2016-02-01 14:46:07
【问题描述】:

我遇到了这个问题:

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

    }

【问题讨论】:

    标签: java android


    【解决方案1】:

    问题出在这一行

    scrollView.addView(linearLayout);
    

    要初始化linearLayout,你使用的是findViewById,这意味着linearLayout已经有一个父级,这样就违反了每个视图只能有一个父级的约束

    【讨论】:

    • 我添加了 linearLayout.removeAllViews();,所以我所做的不正确,对吧?
    • 不是。父母是layoutItemDetail
    • 即使它根本没有意义。如果您可能想重新考虑您在那里所做的事情
    • 谢谢你救了我的命。
    【解决方案2】:

    如果子 View 已经有父级,则不能将任何 View 添加到另一个 View

    在您的情况下,您应该以编程方式创建 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());
            ...
            ...
            ...
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多