<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init(event)">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import com.crap.vo.Teacher;
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable]
private var teacherAC:ArrayCollection=new ArrayCollection();
protected function init(event:FlexEvent):void
{
var teacher:Teacher=new Teacher();
teacher.name="name1";
teacherAC.addItem(teacher);
teacher=new Teacher();
teacher.name="name2";
teacherAC.addItem(teacher);
}
protected function bar(event:MouseEvent):void
{
teacherAC.removeItemAt(0);
}
]]>
</fx:Script>
<s:Button click="bar(event)" label="abc"/>
<s:DataGroup dataProvider="{teacherAC}">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<fx:Script>
<![CDATA[
public override function set data(data:Object):void{
trace(data);
}
]]>
</fx:Script>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:DataGroup>
</s:Application>
上面代码中,当点击了按钮以后,DataGroup的dataProvider的ArrayCollection少了一个项目,这样会触发DataGroup的itemRemoved方法,
里面有
if (oldRenderer is IDataRenderer && oldRenderer !== item)
IDataRenderer(oldRenderer).data = null;
这样就是向ItemRenderer的data setter中传了一个null,不注意的情况下,很容易抛空指针异常.