Question

Im having some problems with an IllegalStateException. I have a function that creates removes all the views from a LinearLayout and recreates and re-adds them. One of these TextViews must be static so that it can interface with a custom view I have created. When that TextView is added to the layout for the second time, the program throws the IllegalStateException. After stepping through the program a bit, It seems that the parent LinearLayout has removed the view from its children, but the TextView itself still "thinks" that it has a parent. Here is my code: (Only the important bits)

 public static TextView latchingText;
 private LinearLayout layout;

 private void recreateLayout(){
      layout.removeView(latchingText);
      layout.addView(latchingtext);
 }

Does anyone have any clue whats going on?

EDIT:Here is the Log:

02-13 09:04:41.012: ERROR/AndroidRuntime(10489): FATAL EXCEPTION: main
02-13 09:04:41.012: ERROR/AndroidRuntime(10489): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.view.ViewGroup.addViewInner(ViewGroup.java:2012)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.view.ViewGroup.addView(ViewGroup.java:1907)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.view.ViewGroup.addView(ViewGroup.java:1864)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.view.ViewGroup.addView(ViewGroup.java:1844)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.PhysicsEngine.Diagram.createTraceLayout(Diagram.java:357)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.PhysicsEngine.Diagram.access$3(Diagram.java:173)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.PhysicsEngine.Diagram$2.onClick(Diagram.java:270)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.view.View.performClick(View.java:2405)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.view.View$PerformClick.run(View.java:8813)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.os.Handler.handleCallback(Handler.java:587)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.os.Looper.loop(Looper.java:123)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at java.lang.reflect.Method.invoke(Method.java:521)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-13 09:04:41.012: ERROR/AndroidRuntime(10489):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

The log say what you need to do.

child.getParent().removeView(child);

This should basically work. Check for null, the getParent() might be null...

A short explanation: The layout variable is not static, so it is very possible that the layout variable you use to call removeView() is already another one and so it is not working for the child. To prevent that you should try to always access the parent by calling getParent() and not only by using a member variable that might already be overridden by a new object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top