Flex Mobile为什么persistNavigatorState =“true”和stage.setAspectRatio(StageAspectRatio.PORTRAIT)不混?

时间:2011-11-15 02:36:01

标签: flex flex4 flexbuilder

为什么我不能在初始化程序中一起使用 persistNavigatorState =“true” stage.setAspectRatio(StageAspectRatio.PORTRAIT)

<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]

每当我启用persistNavigatorState时,我的程序都不会运行。有没有办法一起使用?感谢

[第4行] creationComplete="init()"

    protected  function init():void {


[Line 19]  stage.setAspectRatio(StageAspectRatio.PORTRAIT);

    }

1 个答案:

答案 0 :(得分:2)

也许createComplete事件在舞台可访问之前触发。使用persistNavigatorState首先从持久性缓存中获取导航器的状态信息,从而稍微改变应用程序的启动行为。 Flex文档中的这一行非常有说服力:当应用程序重新启动时,只恢复当前ViewNavigator的状态。此外,在将对象添加到displayList之前,flex中的阶段是不可访问的。因此,如果您在舞台为空时调用setAspectRatio,您将获得一个null obj引用。

您可以尝试在app.xml中设置包含<aspectRatio>portrait</aspectRatio>的内容,而不是在creationComplete上使用stage.setAspectRatio(StageAspectRatio.PORTRAIT)。

或者,您可以在视图中侦听addedToStage事件,并在事件处理程序中调用stage.setAspectRatio(StageAspectRatio.PORTRAIT)。这样你就可以确定舞台是可用的了。

参考文献: