Apache flex Flex Datagrid中的选择不会将valueObject传递给selectionChangeHandler函数
我有一个选项卡导航器,每个选项卡都是一个模块。其中一个模块贴上了“单元”标签,模块的完整代码贴在这篇文章中 有几个问题: 1) 表单不使用datagrid选择中的数据填充。 2) 选择一行并单击“删除”会出现非常常见的错误:Apache flex Flex Datagrid中的选择不会将valueObject传递给selectionChangeHandler函数,apache-flex,actionscript-3,datagrid,value-objects,Apache Flex,Actionscript 3,Datagrid,Value Objects,我有一个选项卡导航器,每个选项卡都是一个模块。其中一个模块贴上了“单元”标签,模块的完整代码贴在这篇文章中 有几个问题: 1) 表单不使用datagrid选择中的数据填充。 2) 选择一行并单击“删除”会出现非常常见的错误:TypeError:error\1009:无法访问空对象引用的属性或方法。 selectionChangeHandler函数中valueObject单元上的跟踪给出NULL。为什么? 注意:在其他模块(TabNavigator的其他选项卡)中,我用单位填充了DropDownL
TypeError:error\1009:无法访问空对象引用的属性或方法。
selectionChangeHandler函数中valueObject单元上的跟踪给出NULL。为什么?
注意:在其他模块(TabNavigator的其他选项卡)中,我用单位填充了DropDownList。这意味着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>
所选对象未成功转换为Unit,这意味着转换之前它可能不是Unit或子类。将它投射到单位不会使它成为单位,除非它以前是单位。你完全正确。临时解决方案是将绑定更改为:
,需要注意的是,如果禁用所有其他选项卡(即不加载其他模块),则发布的模块代码可以正常工作。也就是说,如果unit valueObject用于其他地方,则转换失败。非常奇怪,这里发生的事情是,你的单元代码只在你的模块中被引用,所以无论哪个先加载的都会得到单元的定义,而其他的都有一个单元的“某种”定义。为了使其正常工作,请在shell中使用Unit或确保所有模块都加载到父应用程序域中。^您可以通过将模块的“currentDomain”属性设置为ApplicationDomain.currentDomain
。如果您获得此“common”错误无法访问空对象引用的属性或方法
,是否想过可能您没有进行足够的空检查或没有正确地强制转换对象?