添加视图会导致“Child has have parent”错误

时间:2013-08-28 09:25:15

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

我的问题再一次是为什么这适用于某些设备但不适用于其他设备

1 个答案:

答案 0 :(得分:3)

您可能需要额外处理:

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