java - 添加 View 导致 "Child already has parent"错误

标签 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 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);

ImageButtons 类 thumbs是一个`ArrayList of

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;

public class ImageButtons extends ImageButton{


    Resources res = getResources();


    public ImageButtons(Context context,String img,int id) 
    {
        super(context);
        XmlPullParser parser = res.getXml(R.layout.imagebuttons);
        AttributeSet attributes = null;
        int state = 0;
        while(state != XmlPullParser.END_DOCUMENT)
        {
            try {
                state = parser.next();
            } catch (XmlPullParserException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }       
            if (state == XmlPullParser.START_TAG) {
                if (parser.getName().equals("ImageButton")) {
                   attributes = Xml.asAttributeSet(parser);
                    break;
                }
            }
        } 

        setId(id);

        setLayoutParams(new LayoutParams(context, attributes));
    }


}

竖起大拇指是ArrayList<ImageButton> 第 74 行引用 `imgHolder.addView(thumbs.get(x));

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

最佳答案

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

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

关于java - 添加 View 导致 "Child already has parent"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18484046/

相关文章:

java - 如何将我在java类中创建的ID生成到r.java

android-studio - 删除我的 Android 应用程序中的夜间模式 |安卓工作室

java - 如何将对象添加到我无法编辑的类中

java - 如何通过 XPath 从同一父节点内的文本节点中提取文本

android - 如何使用 "extends Activity"创建自定义 ListView?

Java Google Drive Android API,上传后获取文件ID

java - 使用 map 中的值过滤 pojo 流

java - iReport 生成的 PDF 与 Web App 不同

java - 如何将加速度计传感器的数据写入文件并写入matlab?

android - 用于在 ScaleAnimation 中指定 pivotXType 和 pivotYType 的 XML 属性