为什么.addView抛出此父/子异常? [英] Why does .addView throw this parent/child exception?

查看:70
本文介绍了为什么.addView抛出此父/子异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
我正在尝试在我的主要活动的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.

我尝试过类似问题的答案,但没有成功.
片段-指定的孩子已经有一个父母您必须先在孩子的父母上调用removeView()
首先在孩子的父级上调用removeView()


课:

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

由于它是刚刚创建的,因此没有父项,可以添加为子项

Hello!
I'm trying to create dynamic TextViews inside a LinearLayout within my main activity. The program is (supposed to be) pushing out TextView's from resultrow XML to activity_fivethreeone XML as required. The line parentLayout.addView(textView); is throwing this error;

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

I've tried answers from similar questions but getting no wins.
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);
    }
}

I was trying to use removeView() but couldn't get it to stick.

Any help will be much appreciated!
Thanks!

解决方案

textView has already as parent view, as matter of fact you are able to look for it with findViewById successfully. So this line:

parentLayout.addView(textView);

is causing the exception. You probably want to add view to parentLayout

parentLayout.addView(view);

since it has been just created, it has no parent and can be added as child

这篇关于为什么.addView抛出此父/子异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆