Flex 实例化对象 - 等待创建完成
我创建了一个简单的组件,在主程序中实例化它,如下所示:
newMessage = new MessageDetail();
newMessage.body.text = "Hello World";
我在第二行收到错误“无法访问空对象引用的属性或方法”,因为 newMessage 不完全在尝试设置我的“正文”文本区域的文本之前创建第二行代码。我知道我可以构建一个“creationComplete”事件处理程序,但是没有更简单的方法来做到这一点吗?
I have a simple component I created which I instantiate in my main program like so:
newMessage = new MessageDetail();
newMessage.body.text = "Hello World";
I receive the error "Cannot access a property or method of a null object reference" on the second line because newMessage was not fully created prior to hitting the second line of code trying to set my "body" textarea's text. I know I can build a "creationComplete" event handler, but isn't there a simpler way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根本不要访问子组件。
相反,在您的组件上创建常规的旧属性。让组件将这些值绑定到可视组件。
例如:
MyComponent.MXML:
在您的代码中:
一般来说,我认为默认情况下公开子组件是 Flex 中的一个巨大错误。
Don't access sub-components at all.
Instead make regular old properties on your component. Have the component bind those values to visual components.
For example:
MyComponent.MXML:
In your code:
In general, I believe sub-components being public by default was a huge mistake in Flex.
我建议通过重写方法 createChildren() 来创建组件中的所有子组件。它将确保所有子项都被实例化。更多此处和此处
编辑:
感谢 Michael Brewer-Davis 的评论
I can recommend to create create all children in your component by overriding method createChildren(). It will make sure all children are instantiated. More here and here
EDIT:
Thanks to Michael Brewer-Davis for comment