为什么 .addView 抛出这个 parent/child 异常?

Why does .addView throw this parent/child exception?

您好!
我正在尝试在我的主 activity 内的 LinearLayout 中创建动态 TextView。该程序(应该)根据需要将 TextView 从 resultrow XML 推出到 activity_fivethreeone XML。 parentLayout.addView(textView); 行抛出此错误;

The specified child already has a parent. You must call removeView() on the child's parent first.

我试过类似问题的答案,但没有成功。
Fragments - The specified child already has a parent. You must call removeView() on the child's parent first
Call removeView() on the child's parent first


class:

try {
    LinearLayout parentLayout = (LinearLayout)findViewById(R.id.linLayout);
    LayoutInflater layoutInflater = getLayoutInflater();
    View view;
    for(int counter=0;counter<=theWeeksList.size();counter++){
        view = layoutInflater.inflate(R.layout.resultrow, parentLayout, false);
        TextView textView = (TextView)view.findViewById(R.id.resultRow);
        textView.setText(theWeeksList.get(counter));
        parentLayout.addView(textView);
    }
}

我曾尝试使用 removeView() 但无法坚持使用。

任何帮助将不胜感激!
谢谢!

textView 已经作为父级 view,事实上您可以使用 findViewById 成功查找它。所以这一行:

parentLayout.addView(textView);

导致异常。您可能想将 view 添加到 parentLayout

parentLayout.addView(view);

由于刚刚创建,没有父项,可以添加为子项

您正在尝试添加已属于现有 ViewGroupEditText

删除行

parentLayout.addView(textView);

来自您的代码。你不需要那样做。替换为

parentLayout.addView(view);