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 的其他选项卡)中,我用单位填充了 DropDownLists。这意味着 valueObject Unit 是在其他模块中定义的。然而,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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
selectedObject 未成功转换为 Unit,这意味着在转换之前它可能不是 Unit 或子类。将其转换为单位不会使其成为一个单位,除非它以前是一个单位。
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.