Flex Datagrid中的选择不会将valueObject传递给selectionChangeHandler函数



我有一个TabNavigator,每个选项卡是一个模块。其中一个模块被标记为Units,该模块的完整代码张贴在本文中。

有几个问题:1)表单不使用来自数据网格选择的数据填充。2)选择一行并单击删除会产生非常常见的错误:TypeError: Error #1009: Cannot access a property or method of a null object reference.对selectionChangeHandler函数中的valueObject单元的跟踪给出NULL。为什么?

注意:在其他模块(TabNavigator的其他选项卡)中,我使用单位填充下拉列表。这意味着valueObject单元是在其他模块中定义的。然而,valueObjects应该是模块私有的,而不是共享的。我不知道问题出在哪里。

完整模块代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          xmlns:unitservice="services.unitservice.*"
          xmlns:valueObjects="valueObjects.*"
          width="724"
          height="674">
    <fx:Style source="assets/CAaNDFlex.css"/>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.rpc.events.ResultEvent;
            import spark.events.GridSelectionEvent;
            protected function unitsDg_creationCompleteHandler(event:FlexEvent):void
            {
                getUnitsResult.token=unitservice.getUnits();
            }
            protected function addBtn_clickHandler(event:MouseEvent):void
            {
                currentState="unitsAdd";
                unit=new Unit();
            }
            protected function unitsDg_selectionChangeHandler(event:GridSelectionEvent):void
            {
                trace(event.currentTarget.selectedItem); //Unit object detected
                trace(event.currentTarget.selectedItem as Unit); //NULL 
                trace(unit); // unit is NULL. Why?
                currentState="unitsDetails";
            }

            protected function button_clickHandler(event:MouseEvent):void
            {
                trace(unit); // unit is NULL. Why?
                unit.unitName=unitNameTextInput.text;
                if (unit.unitID == 0)
                {
                    createUnitResult.token=unitservice.createUnit(unit);
                }
                else
                {
                    updateUnitResult.token=unitservice.updateUnit(unit);
                }
            }
            protected function updateBtn_clickHandler(event:MouseEvent):void
            {
                currentState="unitsUpdate";
            }
            protected function createUnitResult_resultHandler(event:ResultEvent):void
            {
                currentState="unitsDetails";
                unit.unitID=event.result as int;
                unitsDg.dataProvider.addItem(unit);
                unitsDg.setSelectedIndex(unitsDg.dataProvider.getItemIndex(unit));
                unitsDg.ensureCellIsVisible(unitsDg.selectedIndex);
            }
            protected function deleteBtn_clickHandler(event:MouseEvent):void
            {
                deleteUnitResult.token = unitservice.deleteUnit(unit.unitID);
            }
            protected function deleteUnitResult_resultHandler(event:ResultEvent):void
            {
                unitsDg.dataProvider.removeItemAt(unitsDg.selectedIndex);
                currentState="units";
            }
        ]]>
    </fx:Script>
    <s:states>
        <s:State name="units"/>
        <s:State name="unitsDetails"/>
        <s:State name="unitsAdd"/>
        <s:State name="unitsUpdate"/>
    </s:states>
    <fx:Declarations>
        <s:CallResponder id="getUnitsResult"
                         result="unit = getUnitsResult.lastResult as Unit"/>
        <unitservice:UnitService id="unitservice"
                                           fault="Alert.show(event.fault.faultString + 'n' + event.fault.faultDetail)"
                                           showBusyCursor="true"/>
        <valueObjects:Unit id="unit" />
        <s:CallResponder id="createUnitResult"
                         result="createUnitResult_resultHandler(event)"/>
        <s:CallResponder id="updateUnitResult"/>
        <s:CallResponder id="deleteUnitResult" result="deleteUnitResult_resultHandler(event)"/>

        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Binding destination="unit" source="unitsDg.selectedItem as Unit"/>
    <s:DataGrid id="unitsDg" x="10" y="37"
                creationComplete="unitsDg_creationCompleteHandler(event)" requestedRowCount="4"
                selectionChange="unitsDg_selectionChangeHandler(event)">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="unitName"
                              headerText="unitName">
                </s:GridColumn>
                <s:GridColumn dataField="unitID"
                              headerText="unitID">
                </s:GridColumn>
            </s:ArrayList>
        </s:columns>
        <s:typicalItem>
            <fx:Object unitID="unitID1"
                       unitName="unitName1">
            </fx:Object>
        </s:typicalItem>
        <s:AsyncListView list="{getUnitsResult.lastResult}"/>
    </s:DataGrid>
    <s:Button id="addBtn" x="10" y="0" label="Add" click="addBtn_clickHandler(event)"
              styleName="actionButton"/>
    <s:Form includeIn="unitsAdd,unitsUpdate"
            x="10"
            y="176"
            defaultButton="{button}">
        <s:FormItem label="unitName">
            <s:TextInput id="unitNameTextInput"
                         text="{unit.unitName}"/>
        </s:FormItem>
        <s:Button id="button"
                  label="Add"
                  click="button_clickHandler(event)"
                  label.unitsUpdate="Update"/>
    </s:Form>
    <s:Button id="updateBtn" x="138" y="0" label="Update" click="updateBtn_clickHandler(event)"/>
    <s:Button id="deleteBtn" x="266" y="0" label="Delete" click="deleteBtn_clickHandler(event)"/>
    <s:Form includeIn="unitsDetails" x="10" y="176">
        <s:FormItem label="unitName">
            <s:Label id="unitNameLabel" text="{unit.unitName}"/>
        </s:FormItem>
    </s:Form>
</s:Module>

selectedObject没有成功地强制转换为Unit,这意味着在强制转换之前它可能不是Unit或子类。将它强制转换为Unit不会使它成为Unit,除非它之前是一个。

相关内容

  • 没有找到相关文章

最新更新