我遇到了 IllegalStateException 的一些问题。我有一个函数,它创建从 LinearLayout 中删除所有视图并重新创建和重新添加它们。这些 TextView 之一必须是静态的,以便它可以与我创建的自定义视图交互。当该 TextView 第二次添加到布局中时,程序会抛出 IllegalStateException。在逐步完成程序之后,似乎父 LinearLayout 已经从其子级中删除了视图,但 TextView 本身仍然“认为”它有一个父级。这是我的代码:(只有重要的部分)
public static TextView latchingText;
private LinearLayout layout;
private void recreateLayout(){
layout.removeView(latchingText);
layout.addView(latchingtext);
}
有人知道发生了什么吗?
编辑:这是日志:
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)