TypeError: Error #1010: 术语尚未定义,并且无任何属性。at mx.controls::List/createItemRenderer()[E:\dev\4.0.0\framew

本文探讨 Flex List 控件中 Error #1010 的成因及解决办法,该错误涉及同时修改 dataProvider 和 itemRenderer 时出现的问题,并给出了一种通过 callLater 方法来规避此问题的实现方式。

纠结的错误,有时候出现有时候不出现==

百度到解决方法,贴出原文。。。

Error #1010: A term is undefined and has no properties.at mx.controls::List/createItemRenderer()

It turns out that there is a weird quirk with the Flex List control and custom itemRenderers.
The problem occurs when you are forced to reset the dataProvider for the list and the itemRenderer for the list at the same time. So, if your code looks like this:

myList.dataProvider = myDataProvider;
myList.itemRenderer = new ClassFactory(MyRenderer);

You will probably get the above error.

What you want to do instead is use the List's callLater() method to set your dataProvider. Your code will now look like this:

myList.itemRenderer = new ClassFactory(MyRenderer);
myList.callLater(myFunction);

private function myFunction():void{
    myList.dataProvider = myDataProvider;
}

Now, I am not entirely sure how this function works, but what I imagine is that this function will call the function specified once the list is ready to accept a dataProvider. ??Again though, I am not entirely sure how it works… I just know that it does.If anyone else has some insight on the callLater() method, please drop some comments to explain.

 

文中所给的解决方法在我这里行不通,但让我知道引发错误的原因是同时修改了列表的dataProvider和itemRenderer,后来想了一下,其实我的itemRenderer一直没变只是重新设置了dataProvider,故将itemRenderer的设置提到最开始,将两者分离,解决该问题。这算回避了问题的根本,还没有找到真正的解决方案。

 

 

 

### 解决 ActionScript 3.0 在 Animate 中运行时报 TypeError #1009 的问题 错误 `TypeError: Error #1009` 表明尝试访问了一个未初始化或者不存在的对象的属性或方法。这种问题通常是由于脚本执行顺序不当、对象未被正确创建或引用路径不正确引起的。 以下是针对该问题的具体分析与解决方案: #### 错误原因剖析 1. **对象未实例化**:如果试图调用某个变量的方法或属性,而这个变量并未指向任何有效的对象,则会出现此类错误[^1]。 2. **加载延迟**:某些情况下,特别是涉及外部文件(如图片、声音或其他 SWF 文件)时,可能因为资源还未完全加载就触发了相关代码而导致异常。 3. **命名冲突或拼写错误**:检查是否有笔误导致找不到预期的对象;另外也要注意区分大小写字母,因为在 AS3 中这是敏感的。 4. **父级容器不可用**:假如某子元件依赖于它的上级显示列表成员存在才能正常工作的话,在这些先决条件满足之前操作它也会引发同样的报错消息。 #### 排查步骤及修复措施 ##### 步骤一:确认所有必要组件均已正确定义并添加到舞台 确保每一个需要用到的 DisplayObject (比如 Sprite 或 MovieClip)都在适当的时候通过 new 运算符构建出来,并且已经成功地附加到了显示列表里去。例如: ```actionscript var mySprite:Sprite = new Sprite(); addChild(mySprite); ``` 如果没有显式地这样做,那么后续对该 sprite 变量的操作就会抛出 error #1009. ##### 步骤二:验证事件绑定时机恰当与否 对于那些需要响应特定动作(像按钮点击)才启动的功能模块来说,应该把它们设置成只会在相应控件确实呈现之后才会激活状态。可以采用 addEventListener 来监听 ADDED_TO_STAGE 事件作为起点之一: ```actionscript this.addEventListener(Event.ADDED_TO_STAGE, initApp); private function initApp(e:Event):void{ removeEventListener(Event.ADDED_TO_STAGE,initApp); // Your initialization code here... } ``` 这样做的好处是可以保证只有当应用程序根节点正式成为可见部分的一分子以后再继续往下走其他流程,从而减少潜在的风险因素。 ##### 步骤三:处理异步数据请求过程中的不确定性 当你从网络获取资料并将结果填充回界面元素之中时,请记得总是要考虑到失败的可能性以及等待期间的状态管理。利用 LoaderInfo 对象配合 complete handler 协助判断素材是否准备完毕后再做进一步安排: ```actionscript loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete); function onLoadComplete(evt:Event){ trace("Content loaded successfully!"); // Proceed with using the content now that it's ready. }; ``` 同时也可以考虑引入 try-catch 结构包裹有可能出现问题的部分以便更好地捕捉意外状况并且给出友好的提示信息而不是让程序崩溃退出。 --- ### 示例修正后的完整代码片段 假设我们有一个简单的搜索功能,其中包含了两个主要组成部分——一个是文本框用于接收用户的查询词条,另一个则是提交按钮负责触发展示匹配项的动作序列。下面是经过改进版本的样子: ```actionscript import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; public class Main extends Sprite { private var searchInput:TextField; public function Main(){ super(); this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } protected function onAddedToStage(event:Event):void { this.removeEventListener(Event.ADDED_TO_STAGE, onAddedTo_stage); initializeUIComponents(); } private function initializeUIComponents():void { if(stage != null && contains(searchInput)){ return ; } searchInput = new TextField(); addChild(searchInput); var submitBtn:SimpleButton = createSubmitButton(); addChild(submitBtn); submitBtn.addEventListener(MouseEvent.CLICK, handleSearchClick); } private function createSubmitButton():SimpleButton { /* Implementation details omitted */ } private function handleSearchClick(event:MouseEvent):void { if(!searchInput || !searchInput.text.trim().length>0 ){ return ;// Prevent errors by checking input validity first. } performSearchOperation(searchInput.text); } private function performSearchOperation(query:String):void { /* Actual logic to execute based upon user query goes here.*/ } } ``` 上面这段代码展示了如何安全有效地组织起基本的应用框架结构,避免因过早接触未经检验过的实体造成 runtime exception 发生几率增加的情况发生。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值