Why can't I use persistNavigatorState="true" and stage.setAspectRatio(StageAspectRatio.PORTRAIT) together in initializing program.
<s:View ...creationComplete="init()">
protected function init():void {
stage.setAspectRatio(StageAspectRatio.PORTRAIT);
}
[SWF] Main.swf - 3,394,828 bytes after decompression
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at views::Try01/init()[C:\Users\James\Adobe Flash Builder 4.5\myProgram\src\views\Try01.mxml:19]
at views::Try01/___Try01_View1_creationComplete()[C:\Users\James\Adobe Flash Builder 4.5\myProgram\src\views\Try01.mxml:4]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at mx.core::UIComponent/set initialized()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]
at mx.managers::LayoutManager/validateClient()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1090]
at mx.core::UIComponent/validateNow()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8067]
at spark.components::ViewNavigator/commitNavigatorAction()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1878]
at spark.components::ViewNavigator/commitProperties()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1236]
at mx.core::UIComponent/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8209]
at mx.managers::LayoutManager/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:597]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:783]
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]
Whenever I enable persistNavigatorState, my program won't run. Is there a way to use both together? Thanks
[Line 4] creationComplete="init()"
protected function init():void {
[Line 19] stage.setAspectRatio(StageAspectRatio.PORTRAIT);
}
Perhaps the creationComplete event fires before the stage is accessable. Using persistNavigatorState changes the startup behavior of your application a bit by first fetching your navigator's state info from the persistence cache. This line in the Flex docs is quite telling: when the application restarts, only the state of the current ViewNavigator is restored. Also, the stage in flex is not accessable until an object is added to the displayList. So if you call setAspectRatio
when the stage is empty, you will get a null obj reference.
Instead of using stage.setAspectRatio(StageAspectRatio.PORTRAIT) on creationComplete, you can try setting including <aspectRatio>portrait</aspectRatio>
in your app.xml.
Or, you could listen for the addedToStage event in your view, and call stage.setAspectRatio(StageAspectRatio.PORTRAIT)
in the event handler. That way you know for sure that the stage is available.
References: