AS3:动态加载图像会导致错误
我正在尝试在 Flex (AS3) 中加载已存在的图像:
使用以下代码:
Img.load('http: //www.path.com/image.jpg');
该路径已验证可以工作,但我总是收到此错误: TypeError:错误#1009:无法访问空对象引用的属性或方法。
我使用了文档中的代码,但仍然收到此错误!
I am trying to load an image in Flex (AS3) which already exists:
<mx:Image id="Img"/>
With this code:
Img.load('http://www.path.com/image.jpg');
The path is verified to work, but I always get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I used the code from the documentation and still receive this error!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您对图像的回答位于视图堆栈的隐藏子级上,因此从其他人的角度来看,这改变了您的问题的性质。通过将视图堆栈的creationPolicy更改为“all”,您实际上可以访问该视图堆栈的子级上的项目,而不使它们可见。
With your answer of the image being on a hidden child of a viewstack, that changes the nature of your problem from other peoples point of view. You can actually access items on the children of that viewstack without making them visible by changing the creationPolicy of the viewstack to "all".
使用 mx.Image 标签时,尝试使用
IMG.source = "http://path.com/image.jpg";
IMG.load();
否则,您可能只需将其设置为 URLRequest;
然而,空对象引用很常见,并且可能来自您在准备好之前尝试访问的许多不同变量。
确保在 Flex 应用程序准备就绪之前您不会尝试访问 IMG。
在您的代码中,在定义文档的 MXML 的第一行上,确保有一个creationComplete="init()" // 或任何您的初始函数,然后在该函数中分配 IMG 源。这确保了 as vars 不会尝试访问 mxml 中尚未准备好的任何内容/
when using an mx.Image tag, try using
IMG.source = "http://path.com/image.jpg";
IMG.load();
otherwise, you might just have to set it up as an URLRequest;
null object references are quite common however, and could come from a number of different vars that you are trying to access before they are ready.
make sure that you aren't trying to access IMG before the flex app is ready.
in your code, on the first line of MXML where you define your document, make sure that there is a creationComplete="init()" // or whatever your initial function is, and then assign the IMG source within that function. this ensures that as vars are not attempting to access anything defined in the mxml that isn't ready yet/
你什么时候调用这个函数?虽然在 Image 控件上调用 load 方法不是标准做法(设置其 source 属性更常见),但这样做应该没问题:
不过,调用该方法的时间确实很重要;如果您在该特定行上获得空引用,则图像控件肯定不存在。
确保至少等到容器的初始化事件触发(如上所述),然后再尝试访问代码中的控件。如果您在运行时动态添加控件,那么您应该等到控件的初始化事件,以确保有一个对象可以使用。
When are you calling the function? And while it's not standard practice to call the load method on an Image control (setting its source property is more common), doing so should be fine:
It does matter when you call that method, though; if you're getting a null reference on that specific line, then the Image control is definitely not there.
Make sure you wait at least until the container's initialize event fires (as above) before attempting to access the control in code. If you're adding the control dynamically at runtime, then you should wait until the control's initialize event, to be sure there's an object there to work with.
好吧,问题是图像组件放置在堆栈视图上的画布上,显然当该画布不可见时您无法访问这些元素
Ok the problem was that the image component was placed on a canvas which was on a stackview, and apparently you cannot access these elements while this canvas is invisible