java.lang.IllegalStateException: 指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7552990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
提问by Kishor datta gupta
This is my code:
这是我的代码:
Frame.gameController.test();
setContentView(Frame.world.getScreen());
Frame.world.setRunning(true);
On the second line I am getting the following error:
在第二行,我收到以下错误:
ERROR/AndroidRuntime(15229): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Can anyone help me solve it? Previously it was working just fine, the problem starts when I take it in another activity.
有人可以帮我解决吗?以前它工作得很好,当我在另一项活动中使用它时问题就开始了。
I'm using android 2.2.
我正在使用安卓 2.2。
回答by Seraphis
Maybe you are trying to set content from objects that already have parent. It looks like you set some views in one activity, for example:
也许您正在尝试从已有父对象的对象中设置内容。看起来您在一个活动中设置了一些视图,例如:
TextView tv = new TextView();
layout.adView(tv);
layout2.adView(tv);
and that error appears when you try to add that tv to different layout. In your situation it's because Layout from one activity is trying to be set as a child in the other activity.
当您尝试将该电视添加到不同的布局时会出现该错误。在您的情况下,这是因为一个活动的布局试图在另一个活动中设置为子活动。
You have to release child from other parent first.
您必须先从其他父母那里释放孩子。
回答by ernazm
You can't use the same view in multiple activities. Instead you should create a new instance of the view.
您不能在多个活动中使用相同的视图。相反,您应该创建视图的新实例。