Archive

Posts Tagged ‘null stage’

FLEX: Why is stage == null?

July 13th, 2009 1 comment

Hello,

If you are trying to access the stage of your application right when your app is completed then you’re doing something wrong.

BAD CODE Most of the cases programmers make the following mistake:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import flash.display.StageDisplayState;
            private function init():void
            {
                var s:Stage = this.stage; //<<-- this.stage == null...why??
                s.scaleMode = StageScaleMode.EXACT_FIT;
            }
        ]]>
    </mx:Script>
</mx:WindowedApplication>

GOOD CODE : The correct approach is the following:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
       xmlns:mx="http://www.adobe.com/2006/mxml"
       creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import flash.display.StageDisplayState;
            private function init():void
            {
                this.systemManager.stage.scaleMode = StageScaleMode.EXACT_FIT;
            }
        ]]>
    </mx:Script>
</mx:WindowedApplication>

Hope we helped you with this issue we also had in our beginning as Flex Devlopers