Java 添加“查看原因”;孩子已经有了父母”;错误

Java 添加“查看原因”;孩子已经有了父母”;错误,java,android,android-layout,android-view,Java,Android,Android Layout,Android View,我正在制作一个画廊应用程序,并使用下面的代码动态添加图像按钮,这在我的一台设备上有效,但在另一台设备上我得到了一个 java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call rem

我正在制作一个画廊应用程序,并使用下面的代码动态添加图像按钮,这在我的一台设备上有效,但在另一台设备上我得到了一个

  java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.  
 Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime( 3358):        at android.view.ViewGroup.addViewInner(ViewGroup.java:3381)
E/AndroidRuntime( 3358):        at android.view.ViewGroup.addView(ViewGroup.java:3252)
E/AndroidRuntime( 3358):        at android.view.ViewGroup.addView(ViewGroup.java:3197)
E/AndroidRuntime( 3358):        at android.view.ViewGroup.addView(ViewGroup.java:3173)
E/AndroidRuntime( 3358):        at package.MainActivity.onCreate(MainActivity.java:74)
错误,但在另一台设备上没有任何问题,运行正常。它运行的是4.1.2,它崩溃的是4.1.1,可能是它吗?该项目的最小目标sdk为16

   setContentView(R.layout.activity_main);

    // Hook up clicks on the thumbnail views.


    imgHolder = (LinearLayout)findViewById(R.id.imgHolder);

    for(int x = 0; x < files.length; x++)
    {
        Log.d("Conor",files[x].getAbsolutePath());
        Drawable img = Drawable.createFromPath(files[x].getAbsolutePath());
        imgs.add(img);
        thumbs.add(new ImageButtons(this, files[x].getAbsolutePath(),x));
        thumbs.get(x).setImageDrawable(img);
        thumbs.get(x).setOnClickListener(onCl);
        imgHolder.addView(thumbs.get(x));
    }
    // Retrieve and cache the system's default "short" animation time.
    mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
thumbs是一个
ArrayList
第74行表示'imgHolder.addView(thumbs.get(x))


我的问题是,为什么这在某些设备上有效,而在其他设备上无效

您可能需要一些额外的处理:

View v = thumbs.get(x);
ViewGroup p = (ViewGroup)v.getParent();
p.removeView(v);
imgHolder.addView(v);

您可能需要一些额外的处理:

View v = thumbs.get(x);
ViewGroup p = (ViewGroup)v.getParent();
p.removeView(v);
imgHolder.addView(v);

请发布整个LogCat堆栈跟踪,并确保我们能够知道异常是从哪一行代码引发的。我们还需要查看有关
thumbs
的信息,因为问题似乎是您试图将
thumbs.get(x)
附加到多个家长。完全有可能不同的不是Android,而是您的运行时环境。我添加了logcat堆栈跟踪,有问题的行是imgHolder.addView(thumbs.get(x))是的,但是
thumbs
的值类型是什么?你们在假设哪段代码是重要的,而你们并没有提供我们所需要的。你们所说的值类型是什么意思?当我在谷歌上搜索它时,在java上下文中找不到对它的引用。关于
thumbs
,您想知道什么?请发布整个类。请发布整个LogCat堆栈跟踪,并确保我们可以知道异常是从代码的哪一行引发的。我们还需要查看有关
thumbs
的信息,因为问题似乎是您试图将
thumbs.get(x)
附加到多个家长。完全有可能不同的不是Android,而是您的运行时环境。我添加了logcat堆栈跟踪,有问题的行是imgHolder.addView(thumbs.get(x))是的,但是
thumbs
的值类型是什么?你们在假设哪段代码是重要的,而你们并没有提供我们所需要的。你们所说的值类型是什么意思?当我在谷歌上搜索它时,在java上下文中找不到对它的引用。关于
thumbs
,你想知道什么?请发布你的整个课程。就是这样,但是我需要在它没有发生的时候添加一个try-catch,但是在它没有发生的时候我需要添加一个try-catch