错误#2006:提供的索引超出范围,在flex中

时间:2010-05-20 13:22:30

标签: actionscript-3 flex flex3

您好我已经为警告框创建了一个组件,我在主应用程序中使用它,`

<mx:Style>    
    .myButton {
        overSkin:Embed("folder/over.png");
        upSkin:Embed(source="folder/up.png");
        downSkin:Embed(source="folder/down.png");
    }
</mx:Style>

<mx:Script>
    <![CDATA[
        import mx.core.Application;
        import mx.managers.PopUpManager;
        import mx.controls.Button;

        [Bindable] public var resultHandler:Function;

        public function creationCompleteHandler():void {
            initializeButtons();
            this.setFocus();
            Application.application.isAlertShowing = true;
        }

        public function initializeButtons():void {
            buttonsContainer.removeAllChildren();

            for( var i:int = 0; i < buttonsCount; i++ ) {
                var button:Button = new Button;
                button.label = buttonsLabels[i];
                button.addEventListener(MouseEvent.CLICK, buttonClickHandler);
                button.styleName = "myButton";

                buttonsContainer.addChild(button);

            }

            Button( buttonsContainer.getChildAt(0) ).setFocus();
        }

        private function buttonClickHandler(event:MouseEvent):void {
            selectedButton = Button(event.currentTarget).label;

            try {
                resultHandler.call();
                this.parent.removeChild(this);
                Application.application.isAlertShowing = false;
            }
            finally {
                ;
            }
        }

        public function enableLargeMode():void {
            canvas.setStyle("backgroundImage", null);
            canvas.setStyle("backgroundColor", 0xffffff);
            canvas.setStyle("borderColor", 0x3b73b9);
            canvas.setStyle("borderStyle", "solid");
            canvas.setStyle("borderThickness", 4);

            titleLabel.setStyle("color", 0x3b73b9);
        }

    ]]>
</mx:Script>

<mx:Number id="buttonsCount">2</mx:Number>

<mx:Array id="buttonsLabels">
    <mx:String>OK</mx:String>
    <mx:String>Yes</mx:String>
    <mx:String>No</mx:String>
</mx:Array>

<mx:String id="selectedButton"/>

<mx:String id="title"/>
<mx:String id="text"/>

<mx:Canvas id="canvas" 
    width="268" height="135" backgroundImage="@Embed('AdvancedAlertAssets/alert-box.png')">

    <mx:Label id="titleLabel" x="18" y="6"
         color="#ffffff" fontWeight="bold" fontSize="16" fontFamily="GE Inspira" 
         text="{title}"/>

    <mx:Text id="textText" left="18" top="36" right="18" height="100%"
        fontFamily="GE Inspira" fontSize="12"
        htmlText="{text}"/>

    <mx:HBox id="buttonsContainer" 
        bottom="18" left="0" right="0" horizontalAlign="center">

    </mx:HBox>

</mx:Canvas>

Now I am using in application like this var advancedAlert:AdvancedAlert = new AdvancedAlert;             this.addChild(advancedAlert);             advancedAlert.title =“标题”;             advancedAlert.text =“text”;             advancedAlert.buttonsCount = 2;             advancedAlert.resultHandler = myAlertHideHandler;             advancedAlert.initializeButtons();             advancedAlert.buttonsLabels [0] =“是”;             advancedAlert.buttonsLabels [1] =“否”;

        function myAlertHideHandler( ):void {
            if( advancedAlert.selectedButton == 'Yes' ) {
                 var myAlerta:AdvancedAlert = new AdvancedAlert();
      **this.addChild(myAlerta);**
      myAlerta.title = "Information";
      myAlerta.text = "Unable to connect to the server";            
      myAlerta.buttonsCount = 1;
      myAlerta.resultHandler = alertListnerFirst;           
      myAlerta.initializeButtons();
            }
        } ` 

显示错误,如

RangeError: Error #2006: The supplied index is out of bounds. at flash.display::DisplayObjectContainer/addChildAt() at mx.core::Container/addChildAt()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:2206] at mx.core::Container/addChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:2140] at main/checkInternetConnection()

我无法理解错误如何解决?请帮助我,提前谢谢。

1 个答案:

答案 0 :(得分:1)

在调用`addChild()以将其附加到新父级之前,您必须在当前父级上调用removeChild()

function myAlertHideHandler( ):void {
    button.removeChild();                
    if( advancedAlert.selectedButton == 'Yes' ) {
         var myAlerta:AdvancedAlert = new AdvancedAlert();
         **this.addChild(myAlerta);**
         myAlerta.title = "Information";
         myAlerta.text = "Unable to connect to the server";            
         myAlerta.buttonsCount = 1;
         myAlerta.resultHandler = alertListnerFirst;           
         myAlerta.initializeButtons();
    }
}