本文介绍了选择在Flex的DataGrid不通过的ValueObject到selectionChangeHandler功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabNavigator容器,每个选项卡是一个模块。其中一个模块的标记单元和模块的全部code被张贴在这篇文章。

有几个问题:1)不填充从DataGrid中选择数据表格。2)选择行并点击删除给出了非常常见的错误:类型错误:错误#1009:无法访问空对象引用的属性或方法在selectionChangeHandler函数中的ValueObject单元上的走线提供了空。为什么呢?

注:在其他模块(与TabNavigator的其他选项卡),我已经填充单元DropDownLists。这意味着该单元的ValueObject在其它模块中定义。然而,valueObjects应该是私有到模块,而不是共享。我不确定哪里出了问题来了。

完整模块code:

 < XML版本=1.0编码=UTF-8&GT?;
< S:模块的xmlns:FX =htt​​p://ns.adobe.com/mxml/2009
          XMLNS:S =库://ns.adobe.com/flex/spark
          的xmlns:MX =库://ns.adobe.com/flex/mx
          的xmlns:unitservice =services.unitservice *。
          的xmlns:valueObjects =valueObjects *。
          宽度=724
          高度=674>
    < FX:样式源=财产/ CAaNDFlex.css/>
    < FX:脚本>
        <![CDATA [
            进口mx.controls.Alert;
            进口mx.events.FlexEvent;
            进口mx.rpc.events.ResultEvent;

            进口spark.events.GridSelectionEvent;

            保护功能unitsDg_creationCompleteHandler(事件:FlexEvent):无效
            {
                getUnitsResult.token = unitservice.getUnits();
            }

            保护功能addBtn_clickHandler(事件:MouseEvent)方法:无效
            {
                currentState的=unitsAdd;
                单位=新的单位();
            }

            保护功能unitsDg_selectionChangeHandler(事件:GridSelectionEvent):无效
            {
                跟踪(event.currentTarget.selectedItem); // Unit对象检测
                跟踪(event.currentTarget.selectedItem为单位); //空值
                跟踪(单位); //单位为NULL。为什么?
                currentState的=unitsDetails;
            }


            保护功能button_clickHandler(事件:MouseEvent)方法:无效
            {
                跟踪(单位); //单位为NULL。为什么?
                unit.unitName = unitNameTextInput.text;
                如果(unit.unitID == 0)
                {
                    createUnitResult.token = unitservice.createUnit(单位);
                }
                其他
                {
                    updateUnitResult.token = unitservice.updateUnit(单位);
                }
            }

            保护功能updateBtn_clickHandler(事件:MouseEvent)方法:无效
            {
                currentState的=unitsUpdate;
            }

            保护功能createUnitResult_resultHandler(事件:的ResultEvent):无效
            {
                currentState的=unitsDetails;
                unit.unitID = event.result为INT;
                unitsDg.dataProvider.addItem(单位);
                unitsDg.setSelectedIndex(unitsDg.dataProvider.getItemIndex(单位));
                unitsDg.ensureCellIsVisible(unitsDg.selectedIndex);
            }

            保护功能deleteBtn_clickHandler(事件:MouseEvent)方法:无效
            {
                deleteUnitResult.token = unitservice.deleteUnit(unit.unitID);

            }

            保护功能deleteUnitResult_resultHandler(事件:的ResultEvent):无效
            {
                unitsDg.dataProvider.removeItemAt(unitsDg.selectedIndex);
                currentState的=单位;
            }

        ]]≥
    < / FX:脚本>
    < S:国家>
        < S:国名=单位/>
        < S:国名=unitsDetails/>
        < S:国名=unitsAdd/>
        < S:国名=unitsUpdate/>
    < / S:国家>
    < FX:声明>
        &所述氏:的CallResponder的id =getUnitsResult
                         结果=单位= getUnitsResult.lastResult为单位/>
        &所述; unitservice:UnitService的id =unitservice
                                           故障=Alert.show(event.fault.faultString +'\ N'+ event.fault.faultDetail)
                                           showBusyCursor =真/>
        < valueObjects:单位ID =单位/>
        &所述氏:的CallResponder的id =createUnitResult
                         结果=createUnitResult_resultHandler(事件)/>
        < S:的CallResponder ID =updateUnitResult/>
        < S:的CallResponder ID =deleteUnitResult结果=deleteUnitResult_resultHandler(事件)/>



        <! - 将非视觉元素(例如,服务,价值的物品)在这里 - >
    < / FX:声明>
    < FX:绑定目标=单位来源=unitsDg.selectedItem为单位/>

    &所述氏:数据网格的id =unitsDg×=10Y =37
                的creationComplete =unitsDg_creationCompleteHandler(事件)requestedRowCount =4
                selectionChange =unitsDg_selectionChangeHandler(事件)>
        < S:列>
            < S:ArrayList的>
                < S:的GridColumn的dataField =的unitName
                              HEADERTEXT =的unitName>
                &所述; /秒:的GridColumn>
                < S:的GridColumn的dataField =unitID
                              HEADERTEXT =unitID>
                &所述; /秒:的GridColumn>
            < / S:ArrayList的>
        < / S:列>
        &所述氏:typicalItem>
            < FX:对象unitID =unitID1
                       的unitName =unitName1>
            < / FX:对象>
        &所述; /秒:typicalItem>
        < S:AsyncListView列表={getUnitsResult.lastResult}/>
    < / S:数据网格>
    < S:按钮的ID =addBtnX =10Y =0的标签=添加点击=addBtn_clickHandler(事件)
              的styleName =actionButton/>
    < S:表includeIn =unitsAdd,unitsUpdate
            X =10
            Y =176
            的defaultButton ={}键>
        < S:FormItem中的标签=的unitName>
            &所述氏:的TextInput的id =unitNameTextInput
                         文本={unit.unitName}/>
        < / S:的FormItem>
        < S:按钮的ID =按钮
                  标签=添加
                  点击=button_clickHandler(事件)
                  label.unitsUpdate =更新/>
    < / S:表>
    < S:按钮的ID =updateBtnX =138Y =0的标签=更新点击=updateBtn_clickHandler(事件)/>
    < S:按钮的ID =deleteBtnX =266Y =0的标签=删除点击=deleteBtn_clickHandler(事件)/>
    &所述氏:表单includeIn =unitsDetails×=10Y =176>
        < S:FormItem中的标签=的unitName>
            < S:标签ID =unitNameLabel文本={unit.unitName}/>
        < / S:的FormItem>
    < / S:表>
< / S:模块>
 

解决方案

该selectedObject没有成功铸造单位,这意味着它可能不是单位或前投一个子类。它铸造到机不能成为一个单元,除非它是一前。

I have a TabNavigator, and each tab is a Module. One of the modules is labelled Units and the full code of the module is posted in this post.

There are several problems:1) Forms are not populated with data from the datagrid selection.2) Selecting a row and clicking delete gives the very-common error: TypeError: Error #1009: Cannot access a property or method of a null object reference. A trace on the valueObject unit within the selectionChangeHandler function gives NULL. Why?

Note: In other modules (other tabs of the TabNavigator), I have DropDownLists populated with units. This means that the valueObject Unit is defined in the other modules. However, valueObjects should be private to modules, and not shared. I am unsure where the problem comes.

Full module code:

<?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>
解决方案

The selectedObject is not successfully casting to Unit, which means it probably wasn't Unit or a subclass prior to the cast. Casting it to Unit won't make it a Unit unless it was one before.

这篇关于选择在Flex的DataGrid不通过的ValueObject到selectionChangeHandler功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:47