Android 中的 IllegalStateException

发布于 2024-10-17 07:56:28 字数 2763 浏览 2 评论 0原文

我遇到了一些 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-10-24 07:56:28

日志说明了您需要做什么。

child.getParent().removeView(child);

这基本上应该可以工作。检查 null,getParent() 可能为 null...

简短说明:布局变量不是静态的,因此您用来调用 removeView() 的布局变量很可能已经是静态的另一种,所以它对孩子不起作用。为了防止这种情况,您应该尝试始终通过调用 getParent() 来访问父级,而不仅仅是使用可能已被新对象覆盖的成员变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文