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

走远了吗. 提交于 2019-12-22 04:57:43

问题


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.


回答1:


You can't use the same view in multiple activities. Instead you should create a new instance of the view.




回答2:


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.



来源:https://stackoverflow.com/questions/7552990/java-lang-illegalstateexception-the-specified-child-already-has-a-parent-you-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!