Flex 4.5 Mobile 中的自定义着色错误

发布于 2024-11-18 23:33:02 字数 3731 浏览 2 评论 0原文

当值 < 时,我尝试将数据着色为红色。 0,否则为绿色。我用来显示列,我的 itemRenderer 操作脚本文件给出错误“无法访问 null 对象的属性”。在我的 as 文件中,当我调试时,我可以看到“data”的值变量为 null ,一旦它调用超级构造函数,它就会给出此错误。

我的操作脚本文件

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); 

    }
}

来自 mxml 文件的调用是:

                <s:GridColumn id="name1" dataField="name" headerText="Name" />
                <s:GridColumn dataField="excess_return" headerText="Excess Return" itemRenderer="util.ConditionalColoredLabelAS"/>

错误是

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\framework\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]

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\framework\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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-11-25 23:33:02

终于找到答案


函数是

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>

声明是

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文