Android异常“指定的孩子已经有一个父母......"

Android Exception quot;The specified child already has a parent...quot;(Android异常“指定的孩子已经有一个父母......)
本文介绍了Android异常“指定的孩子已经有一个父母......"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到如下异常.有人可以帮帮我吗?

I m getting exception as follow. Can anybody help me?

06-16 11:32:48.237: 错误/AndroidRuntime(9223):java.lang.IllegalStateException:指定的孩子已经有一个父母.您必须先在孩子的父母上调用 removeView().

06-16 11:32:48.237: ERROR/AndroidRuntime(9223): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.view.ViewGroup.addView(ViewGroup.java:1865)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.view.ViewGroup.addView(ViewGroup.java:1822)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.view.ViewGroup.addView(ViewGroup.java:1802)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at com.mVisum_philips.MainECGActivity.popupEvent(MainECGActivity.java:628)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at com.mVisum_philips.MainECGActivity$1.onClick(MainECGActivity.java:195)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.view.View.performClick(View.java:2408)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.view.View$PerformClick.run(View.java:8816)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.os.Handler.handleCallback(Handler.java:587)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.os.Handler.dispatchMessage(Handler.java:92)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.os.Looper.loop(Looper.java:123)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at android.app.ActivityThread.main(ActivityThread.java:4627)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at java.lang.reflect.Method.invokeNative(Native Method)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at java.lang.reflect.Method.invoke(Method.java:521)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        06-16 11:32:48.237: ERROR/AndroidRuntime(9223):     at dalvik.system.NativeStart.main(Native Method)

screen_pop_up.xml 的源代码:

<?xml version="1.0" encoding="utf-8"?>
           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
            <LinearLayout android:id="@+id/headerLin" android:layout_width="fill_parent" android:layout_height="wrap_content">
                <RelativeLayout android:layout_height="wrap_content" android:id="@+id/relativeLayout1" android:layout_width="fill_parent">
                    <Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:text="Done"></Button>
                    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_alignParentRight="true">
                        <Button android:layout_height="wrap_content" android:background="@drawable/h_scale" android:id="@+id/button2" android:layout_width="wrap_content" android:padding="15sp"></Button>
                        <Button android:layout_height="wrap_content" android:background="@drawable/v_scale" android:id="@+id/button3" android:layout_width="wrap_content" android:padding="14sp"></Button>
                    </LinearLayout>
                </RelativeLayout>
           </LinearLayout>


        <LinearLayout android:id="@+id/linLin" android:layout_height="300sp" android:layout_width="250sp">
        </LinearLayout>
        </LinearLayout>

我得到异常的源代码如下:

Source code where i m getting the exception is as follows:

final Dialog myDialog = new Dialog(MainECGActivity.this);
                myDialog.setContentView(R.layout.screen_pop_up);
                myDialog.setTitle("My Dialog");

                LinearLayout myLin = (LinearLayout) myDialog.findViewById(R.id.linLin);
                **myLin.addView(ECGField.get(0));**

                myDialog.setCancelable(true);
                Button button = (Button) myDialog.findViewById(R.id.button1);
                button.setOnClickListener(new OnClickListener() {
                @Override
                    public void onClick(View v) {
                    myDialog.dismiss();
                    }
                });

                myDialog.show();

我在这里经历了很多线程,但我错过了一些东西.提前致谢.

I have gone through many threads here but I am missing something. Thanks in advance.

推荐答案

视图只能有一个父视图.您正在添加的视图(我猜是重用)已经是另一个视图层次结构的一部分.如果您真的想重用它(我建议您可能不要),那么您必须将它与现有视图层次结构中的父级分离.

A view can only have a single parent. The view that you are adding (I am guessing re-using) is already part of another view hierarchy. If you really want to reuse it (I would suggest you probably dont) then you have to detach it from its parent in its existing view hierarchy.

ECGField.get(0) 返回的内容已经在视图层次结构中.

whatever is returned by ECGField.get(0) is already in a view hierarchy.

这篇关于Android异常“指定的孩子已经有一个父母......"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)