程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-addView同一個對象兩次會出錯

android-addView同一個對象兩次會出錯

編輯:編程綜合問答
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過一次,就會報錯,請問是因為什麼?有什麼解決辦法?

最佳回答:


你可以去看看源碼,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();
    }
}




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved