AS3:动态加载图像会导致错误

发布于 2024-08-19 22:58:48 字数 264 浏览 5 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆之渊 2024-08-26 22:58:48

由于您对图像的回答位于视图堆栈的隐藏子级上,因此从其他人的角度来看,这改变了您的问题的性质。通过将视图堆栈的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".

独木成林 2024-08-26 22:58:48

使用 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/

在风中等你 2024-08-26 22:58:48

你什么时候调用这个函数?虽然在 Image 控件上调用 load 方法不是标准做法(设置其 source 属性更常见),但这样做应该没问题:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onInitialize()">

    <mx:Script>
        <![CDATA[

            private function onInitialize():void
            {
                Img.load("http://turbonerd.com/media/images/roaming/o/20100203192528.jpg");
            }

        ]]>
    </mx:Script>

    <mx:Image id="Img" />

</mx:Application>

不过,调用该方法的时间确实很重要;如果您在该特定行上获得空引用,则图像控件肯定不存在。

确保至少等到容器的初始化事件触发(如上所述),然后再尝试访问代码中的控件。如果您在运行时动态添加控件,那么您应该等到控件的初始化事件,以确保有一个对象可以使用。

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:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onInitialize()">

    <mx:Script>
        <![CDATA[

            private function onInitialize():void
            {
                Img.load("http://turbonerd.com/media/images/roaming/o/20100203192528.jpg");
            }

        ]]>
    </mx:Script>

    <mx:Image id="Img" />

</mx:Application>

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.

漫漫岁月 2024-08-26 22:58:48

好吧,问题是图像组件放置在堆栈视图上的画布上,显然当该画布不可见时您无法访问这些元素

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文