使用ItemRendering和spark GridColumn时的TypeError

时间:2011-07-28 11:32:21

标签: flex flex4 flexbuilder itemrenderer flex4.5

我正在渲染GridColumn的{​​{1}},如下所示:

DataGrid

渲染器类[FlightRenderer.mxml]:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               initialize="doSend()">
    <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:HTTPService id="xmlFromDatabase" method="GET"
                       result="resultHandler(event)" showBusyCursor="true"
                       url="http://localhost:81/PHP-Pro/mys.php"/>
    </fx:Declarations>
    <fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.core.ClassFactory;
        import mx.rpc.events.ResultEvent;

        import spark.skins.spark.DefaultItemRenderer;
        import spark.utils.DataItem;
        [Bindable]
        private var datalist:ArrayCollection;
        private var mTimer:Timer = new Timer(1000,10);

        private function Countsec(e:TimerEvent):void
        {
            lbltime.text=e.target.currentCount;
        }
        private function Comple(e:TimerEvent):void
        {
            lbltime.text="Time Complete";
        }
        private function resultHandler(event:ResultEvent):void{
            mTimer.start();
            mTimer.addEventListener(TimerEvent.TIMER,Countsec);
            mTimer.addEventListener(TimerEvent.TIMER_COMPLETE,Comple);
            datalist = event.result.data.row;
        }
        public function doSend():void {
            xmlFromDatabase.url = "http://localhost:81/PHP-Pro/mys.php";
            xmlFromDatabase.send(); 
        }

        private function FItem_renderer(item:Object, column:GridColumn):ClassFactory{
            //the main logic is here when the value of GridColumn is="SBI" i 
            //should call my user defined renderer else default render
            if (item.FNo=="SBI"){//not working here
                return new ClassFactory(FlightRenderer);    
            } else {
                return new ClassFactory(DefaultItemRenderer);
            }   
        }
        ]]>
    </fx:Script>
    <s:Panel left="20" top="20" horizontalCenter="true"
             title="Просмотр рейсов" verticalCenter="true">
        <s:layout>
            <s:FormLayout/>
        </s:layout>
        <s:mxmlContentFactory>
            <s:DataGrid id="dg" x="10" y="10" width="708" height="272" color="#01064D"
                        dataProvider="{datalist}" fontFamily="tahoma">
                <s:columns>
                    <s:ArrayList>
                        <s:GridColumn dataField="data" headerText="Дата" itemRendererFunction="FItem_renderer"/>
                        <s:GridColumn dataField="FNo" headerText="№рейса" itemRendererFunction="FItem_renderer"/>
                        <s:GridColumn dataField="name_ru" headerText="Город" itemRendererFunction="FItem_renderer"/>
                        <s:GridColumn dataField="AirCraft" headerText="ВС" itemRendererFunction="FItem_renderer" />
                        <s:GridColumn dataField="FTime" headerText="Время" itemRendererFunction="FItem_renderer"  />
                        <s:GridColumn dataField="Stat" headerText="Статус" itemRendererFunction="FItem_renderer"/>
                    </s:ArrayList>
                </s:columns>
            </s:DataGrid>
        </s:mxmlContentFactory>
    </s:Panel>
    <s:Label id="lbltime" x="41" y="347" fontSize="30" fontWeight="bold" text="Label"/>
</s:Application>

当我运行此应用程序时,会发生以下错误:

<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                    xmlns:s="library://ns.adobe.com/flex/spark" 
                    xmlns:mx="library://ns.adobe.com/flex/mx"
                    clipAndEnableScrolling="true">
    <fx:Script>
    <![CDATA[
        override public function prepare(hasBeenRecycled:Boolean):void {
            lblData.text = data[column.dataField]
        }
    ]]>
    </fx:Script>
    <s:Rect top="0" bottom="0" right="0" left="0">
        <s:fill>
            <s:SolidColor color="#E0E0E0" alpha="0.5"/>
        </s:fill>
    </s:Rect>
    <s:Label id="lblData" top="9" left="7" color="0x505050" alpha="0.5"/>
</s:GridItemRenderer>

我知道我滥用覆盖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/validateSize()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:665] at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:792] at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180] 并且不知道要声明什么是输入参数:

defaultItemRender

如何检查此函数中的private function FItem_renderer(item:Object, column:GridColumn):ClassFactory 值?

2 个答案:

答案 0 :(得分:2)

如果没有看到您的服务器返回的内容,请尝试此操作。在初始化数据提供程序之前,为if语句Null安全。此外,您可能希望将DefaultItemRenderer更改为DefaultGridItemRenderer,

private function FItem_renderer(item:Object, column:GridColumn):ClassFactory{
                //the main logic is here when the value of GridColumn is="SBI" i 
                //should call my user defined renderer else default render
                if (item && item.FNo=="SBI"){//not working here
                    return new ClassFactory(FlightRenderer);    
                }else
                {
                    return new ClassFactory(DefaultGridItemRenderer);
                }   
            }

答案 1 :(得分:1)

看起来好像是在设置不正确的数据。您应该通过覆盖data设置器来设置数据,或者在这种情况下,只需将data直接绑定到Label的{​​{1}}属性,如下所示:

text

同时取消对<s:Label id="lblData" text="{data}" top="9" left="7" color="0x505050" alpha="0.5"/>

的覆盖