开发者

AS3: loading image dynamically results in error

开发者 https://www.devze.com 2022-12-18 23:04 出处:网络
I am trying to load an image in Flex (AS3) which already exists: <mx:Image id=\"Img\"/> With this code:

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开发者_JS百科.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!


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".


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/


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消