views:

33

answers:

3

Hi all, I've got a problem in an ItemRenderer in Flex 3.5. I've looked at the other posts regarding this error but still can't figure it out. The ItemRenderer is part of an AdvancedDataGrid who's data provider is HierarchicalData. I'm getting the ArgumentError but the trace doesn't go to any of my code. I've gone through in debug mode tons of times but it looks like it doesn't happen until after my code runs. Quite strange.

The item renderer has a couple different parts. It figures out what row it should be drawing for based on the xml data and then adds labels and sprites appropriately. If anyone can help, that would be a great help! Thanks!

Here is one of the methods that gets called if the itemrenderer is on a certain row.

private function addLabels(planList:ArrayCollection):void {
     height = 0;
     var sprite:Sprite = new Sprite();
     var curX:Number = (width / planList.length);
     height = 110;
     for each (var plan:Plan in planList) {
        var label:Label = new Label();
        label.text = plan.planner.label;

        label.rotationZ = 270;
        label.visible = true;
        label.x = curX - 7;

        //Draw line divider
        curX += (width / planList.length);
        addChild(label);
        label.move(label.x, height - 30);

        //Draw divider line
        sprite.graphics.lineStyle(.5, 0x000000);
        sprite.graphics.moveTo(label.x - ((width / planList.length) / 2) + 10.5, 0);
        sprite.graphics.lineTo(label.x - ((width / planList.length) / 2) + 10.5, height - 28);

        //Draw meatball
        sprite.graphics.beginFill(0x00FF33);
        sprite.graphics.drawCircle(label.x + 10, height - 15, 10);
        sprite.graphics.endFill();
     }
     rawChildren.addChild(sprite);

  }

There's another function that gets called on a different row, but if I comment out the code above everything works fine, so my guess is that the problem definitely lies there. Thanks for the help!

Here is where addLabels gets called:

override protected function createChildren():void {
     removeAllChildren();
     var count:int = rawChildren.numChildren;
     for (var i:int = 0; i < count; i++) {
        if (rawChildren.getChildAt(0).parent) {
           rawChildren.removeChildAt(0);
        }
     }
     var allPlans:ArrayCollection = new ArrayCollection();
     if (_plan) {
        getAllPlans(_plan, allPlans);
     }
     if (_name == "capability") {

     }
     else if (_name == "components") {
        height = 130;
        width = 335;
        addLabels(allPlans);  // <-- RIGHT HERE!
        var sprite:Sprite = new Sprite();
        sprite.graphics.lineStyle(.5, 0x000000);
        sprite.graphics.moveTo(0, 0);
        sprite.graphics.lineTo(width, 0);

        sprite.graphics.moveTo(0, height - 28);
        sprite.graphics.lineTo(width, height - 28);
        rawChildren.addChild(sprite);
     }
  }
A: 

I've seen this kind of thing before. This error could be happening in the AdvancedDataGrid itself, not the itemRenderer.

See http://www.judahfrangipane.com/blog/?p=196 for more information.

If you simply want to draw something on specific rows, you might try extending the AdvancedDataGrid to override two functions:

override protected function drawHorizontalLine(s:Sprite, rowIndex:int, color:uint, y:Number):void {
  // do some drawing 
}

override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void {
  // do some drawing 
}
Robusto
I think you're right about that actually. I posted the stack trace below and it definitely references AdvancedDataGrid.
Still a little confused... So you think the error might be occurring because the data grid thinks the labels and sprites should be children of it, and not of the item renderer?
Yes, something like that. I don't know exactly how you're calling addLabels() but it seems your contexts are getting confused.
Robusto
I edited the question to show where addLabels is getting called. It's in the createChildren function.
Also, the ItemRenderer extends Canvas
Ok, I've narrowed it down a little bit further. The error happens if I add the label as a child. Specifically this line: addChild(label);Is there some other thing I can add this label as a child to?
A: 

Here is the full stack trace:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/getChildIndex()
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::rawChildren_getChildIndex()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Container.as:3929]
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::rawChildren_removeChild()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Container.as:3863]
at mx.core::ContainerRawChildrenList/removeChild()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\ContainerRawChildrenList.as:159]
at mx.core::Container/createOrDestroyScrollbars()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Container.as:4493]
at mx.core::Container/createScrollbarsIfNeeded()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Container.as:4402]
at mx.core::Container/createContentPaneAndScrollbarsIfNeeded()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Container.as:4218]
at mx.core::Container/validateDisplayList()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Container.as:2734]
at mx.managers::LayoutManager/validateClient()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\LayoutManager.as:880]
at mx.core::UIComponent/validateNow()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\UIComponent.as:5679]
at mx.controls.advancedDataGridClasses::AdvancedDataGridBase/updateDisplayOfItemRenderer()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\advancedDataGridClasses\AdvancedDataGridBase.as:970]
at mx.controls::AdvancedDataGrid/updateDisplayOfItemRenderer()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\AdvancedDataGrid.as:1900]
at mx.controls.advancedDataGridClasses::AdvancedDataGridBase/drawItem()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\advancedDataGridClasses\AdvancedDataGridBase.as:957]
at mx.controls.advancedDataGridClasses::AdvancedDataGridBase/drawVisibleItem()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\advancedDataGridClasses\AdvancedDataGridBase.as:1009]
at mx.controls::AdvancedDataGrid/drawVisibleItem()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\AdvancedDataGrid.as:7251]
at mx.controls.advancedDataGridClasses::AdvancedDataGridBase/createRow()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\advancedDataGridClasses\AdvancedDataGridBase.as:1673]
at mx.controls.advancedDataGridClasses::AdvancedDataGridBase/makeRowsAndColumns()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\advancedDataGridClasses\AdvancedDataGridBase.as:867]
at mx.controls::AdvancedDataGridBaseEx/makeRowsAndColumns()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\AdvancedDataGridBaseEx.as:1919]
at mx.controls::AdvancedDataGrid/makeRowsAndColumns()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\AdvancedDataGrid.as:7172]
at mx.controls.listClasses::AdvancedListBase/makeRowsAndColumnsWithExtraRows()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\listClasses\AdvancedListBase.as:1225]
at mx.controls.listClasses::AdvancedListBase/updateDisplayList()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\listClasses\AdvancedListBase.as:3495]
at mx.controls::AdvancedDataGridBaseEx/updateDisplayList()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\AdvancedDataGridBaseEx.as:1779]
at mx.controls::AdvancedDataGrid/updateDisplayList()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\AdvancedDataGrid.as:6010]
at mx.controls.listClasses::AdvancedListBase/validateDisplayList()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\controls\listClasses\AdvancedListBase.as:3066]
at mx.managers::LayoutManager/validateDisplayList()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\LayoutManager.as:622]
at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\LayoutManager.as:695]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\UIComponent.as:8733]
at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\UIComponent.as:8673]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
A: 

Here's the answer if anyone was looking at this.. I'm still not exactly sure what the problem was, but the AdvancedDataGrid certainly did not like me adding a Label as a child to the renderer. Here's how I got around it.. add a TextField as a child to the sprite, as shown:

        var newLabel:TextField = new TextField();
        newLabel.text = plan.planner.label;
        newLabel.rotationZ = 270;
        newLabel.visible = true;
        newLabel.x = curX - (dividerWidth / 2) - ((newLabel.textHeight) / 1.5);
        newLabel.y = height - (RADIUS * 2.5);
        newLabel.antiAliasType = AntiAliasType.ADVANCED;

        sprite.addChild(newLabel);

Hope this helps someone!