Custom coloring error in flex 4.5 Mobile
I am trying to colour data red when the value < 0, otherwise green. I am using to display columns and my itemRenderer actions script file gives me error "cannot access property of a null
object,. In my as file, when I debug, I can see the value of "data" variable as null
and as soon as it calls the super constructor it gives this error.
My action script file
public class ConditionalColoredLabelAS extends LabelItemRenderer
{
public function ConditionalColoredLabelAS() {
if (data !=null)
super();
}
// Use the value of the myColor property to draw
// the background color of the item in the list.
override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void {
// Define a var to hold the color.
var myColor:uint;
if (data !=null)
{
// Determine the RGB color value from the label property.
if (data == "red")
myColor = 0xFF0000;
if (data == "green")
myColor = 0x00FF00;
if (data == "blue")
myColor = 0x0000FF;
}
//graphics.beginFill(myColor, 1);
//graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
}
}
My call from the mxml file is :
<s:GridColumn id="name1" dataField="name" headerText="Name" />
<s:GridColumn dataField="excess_return" headerText="Excess Return" itemRenderer="util.ConditionalColoredLabelAS"/>
Error is
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at spark.components::Group/addElement()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\Group.as:1342]
at spark.components.gridClasses::GridLayout/createTypicalItemRenderer()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\gridClasses\GridLayout.as:748]
at spark.components.gridClasses::GridLayout/updateTypicalCellSizes()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\gridClasses\GridLayout.as:883]
at spark.components.gridClasses::GridLayout/measure()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\gridClasses\GridLayout.as:444]
at spark.components.supportClasses::GroupBase/measure()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\supportClasses\GroupBase.as:1148]
at mx.core::UIComponent/measureSizes()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8496]
at mx.core::UIComponent/validateSize()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8420]
at spark.components::Group/validateSize()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\Group.as:1012]
at mx.managers::LayoutManager/validateClient()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:987]
at mx.core::UIComponent/validateNow()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8067]
at spark.components::ViewNavigator/commitNavigatorAction()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1878]
at spark.components::ViewNavigator/commitProperties()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1236]
at mx.core::UIComponent/validateProperties()[E:\dev\4.5.1\frameworks\projects\f开发者_如何学Goramework\src\mx\core\UIComponent.as:8209]
at mx.managers::LayoutManager/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:597]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:783]
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]
Finally found the answer
The function is
private function returnItemRenderer( item: Object, column: GridColumn ): IFactory
{
if (item == null)
return default_grid_item_renderer;
//var field:String = column.dataField;
if(item._return < 0)
return red_grid_item_renderer;
else if (item._return>0)
return green_grid_item_renderer;
return default_grid_item_renderer;
//return new ClassFactory( DefaultGridItemRenderer );
}
]]>
</fx:Script>
The declarations are
<fx:Declarations>
<fx:Component id="default_grid_item_renderer" >
<s:DefaultGridItemRenderer />
</fx:Component>
<fx:Component id="red_grid_item_renderer" >
<s:DefaultGridItemRenderer color="0xFF0000"/>
</fx:Component>
<fx:Component id="green_grid_item_renderer" >
<s:DefaultGridItemRenderer color="0x00FF00"/>
</fx:Component>
</fx:Declarations>
<s:DataGrid id="top5Grid" width="100%" height="100%"
dataProvider="{getDataResultMax.lastResult}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="impact" headerText="Impact" itemRendererFunction="impactItemRenderer"/>
<s:GridColumn dataField="_return" headerText="Return" itemRendererFunction="returnItemRenderer"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
精彩评论