Jason_White
Jason_White
采纳率60%
2015-06-08 10:14 阅读 2.6k

addView同一个对象两次会出错

        //首页按钮
        ImageView home=new ImageView(context);
        home.setImageResource(R.drawable.icon_03);
        home.setLayoutParams(iconparams);
        home.setPadding(10, 10, 10, 10);
        addView(home);

        //分隔符
        ImageView seprator = new ImageView(context);
        seprator.setImageResource(R.drawable.seprator_03);
        seprator.setLayoutParams(sepratorparams);
        addView(seprator);

        //2
        ImageView icon2=new ImageView(context);
        icon2.setImageResource(R.drawable.icon_05);
        icon2.setLayoutParams(iconparams);
        icon2.setPadding(10, 10, 10, 10);
        addView(icon2);
        addView(seprator);

上边是我在android项目中写的一个用户控件的部分代码,调试后发现再第二次addView(seprator);时候会报错,然后我试了一下其他的,结果是只要是第二次addView的参数对象之前已经addView过一次,就会报错,请问是因为什么?有什么解决办法?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    u014300915 依风听雨锋 2015-06-08 10:52

    你可以去看看源码,ViewGroup→addView()→addViewInner()中

    private void addViewInner(View child, int index, LayoutParams params,
    boolean preventRequestLayout) {

        if (mTransition != null) {
            // Don't prevent other add transitions from completing, but cancel remove
            // transitions to let them complete the process before we add to the container
            mTransition.cancel(LayoutTransition.DISAPPEARING);
        }
    
        if (child.getParent() != null) {
            throw new IllegalStateException("The specified child already has a parent. " +
                    "You must call removeView() on the child's parent first.");
        }
    
        if (mTransition != null) {
            mTransition.addChild(this, child);
        }
    
        if (!checkLayoutParams(params)) {
            params = generateLayoutParams(params);
        }
    
        if (preventRequestLayout) {
            child.mLayoutParams = params;
        } else {
            child.setLayoutParams(params);
        }
    
        if (index < 0) {
            index = mChildrenCount;
        }
    
        addInArray(child, index);
    
        // tell our children
        if (preventRequestLayout) {
            child.assignParent(this);
        } else {
            child.mParent = this;
        }
    
        if (child.hasFocus()) {
            requestChildFocus(child, child.findFocus());
        }
    
        AttachInfo ai = mAttachInfo;
        if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
            boolean lastKeepOn = ai.mKeepScreenOn;
            ai.mKeepScreenOn = false;
            child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
            if (ai.mKeepScreenOn) {
                needGlobalAttributesUpdate(true);
            }
            ai.mKeepScreenOn = lastKeepOn;
        }
    
        if (child.isLayoutDirectionInherited()) {
            child.resetRtlProperties();
        }
    
        onViewAdded(child);
    
        if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
            mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
        }
    
        if (child.hasTransientState()) {
            childHasTransientStateChanged(child, true);
        }
    
        if (child.getVisibility() != View.GONE) {
            notifySubtreeAccessibilityStateChangedIfNeeded();
        }
    }
    
    
    
    
    
    
    
    
    
    点赞 评论 复制链接分享
  • bdmh bdmh 2015-06-08 11:00

    没错,因为它已经有parent了,你要先removeallviews,然后再添加

    点赞 评论 复制链接分享
  • hikyson Android_Ky 2015-06-09 01:19

    同一个对象添加两次当然会崩溃了,会提示已经有父view

    如果你想添加两次都构造两个seperator对象!!!new 两次就可以了

    点赞 评论 复制链接分享
  • Arnold9009 Arnold9009 2015-06-09 02:03

    每次都有new一个新的view来add

    点赞 评论 复制链接分享
  • u011133213 胖虎 2015-06-09 11:06

    已经存在的view 具有一个parent,会报错的。或者你需要移除他的parentView

    点赞 评论 复制链接分享
  • xby1993 小小懒羊羊 2015-06-09 16:53

    这个问题前面几位已经回答很详细了。我的建议:遇到奇怪的问题,如API文档中没有找到原因。这个时候就应该看看源代码了。

    点赞 评论 复制链接分享

相关推荐