7

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.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
Kishor datta gupta
  • 1,103
  • 2
  • 14
  • 42

2 Answers2

11

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.

Seraphis
  • 1,026
  • 2
  • 14
  • 30
10

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

ernazm
  • 9,208
  • 4
  • 44
  • 51
  • 1
    can u give me an example, any link? to how to do it proper way? – Kishor datta gupta Sep 26 '11 at 09:24
  • 1
    @KISHORDATTAGUPTA it look like Frame.world.getScreen() is getting a shared instance. You should probably create a new instance instead. – ThomasW Sep 26 '11 at 09:29
  • An example for what? You gave us almost no code, no purposes. I just explained you the reasong you are getting the exception. – ernazm Sep 26 '11 at 09:31
  • @emjam, thanks for explaining. but i very new in android ,so i dont understand how my only activity get shared. – Kishor datta gupta Sep 26 '11 at 09:35
  • You said you retrieved the view from another activity. Instead of retrieving the view you'd better inflate a new one. Or, maybe you don't need the second activity and should stick to a single activity. All depends on your task. – ernazm Sep 26 '11 at 09:39
  • i have another activity, which i am not calling.in only activity i m running the view. if the first activity i call, i m going to get the error right? – Kishor datta gupta Sep 26 '11 at 09:48