2 Replies Latest reply on Jul 4, 2009 1:49 PM by vertmonkee

    Native Menu

    vertmonkee

      I am trying to follow an example of a very simple menu from a book but it gives me an error message and I'm unsure why.

       

      Can anyone shed some light on it please.

       

      My code is as follows

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
          layout="absolute">
         
          <mx:Script>
              <![CDATA[

       

                  //Create the root menu
                  public var exampleMenu:NativeMenu = new NativeMenu();
                 
                  //Create a sub item for the root menu
                  public var item:NativeMenuItem = new NativeMenuItem("Example item");
                 
                  //Add the sub item to the root menu
                  exampleMenu.addItem(item);
              ]]>
          </mx:Script>

       

          <mx:Label id="welcomeMessage" text="Welcome to my desktop app" />
         
      </mx:WindowedApplication>

       

      I then tried adding in import statements, although these did get added even when I used the autocomplete from the code hinting

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
          layout="absolute">
         
          <mx:Script>
              <![CDATA[
             
                  import flash.display.NativeMenu;
                  import flash.display.NativeMenuItem;
                 
                  //Create the root menu
                  public var exampleMenu:NativeMenu = new NativeMenu();
                 
                  //Create a sub item for the root menu
                  public var item:NativeMenuItem = new NativeMenuItem("Example item");
                 
                  //Add the sub item to the root menu
                  exampleMenu.addItem(item);
              ]]>
          </mx:Script>

       

          <mx:Label id="welcomeMessage" text="Welcome to my desktop app" />
         
      </mx:WindowedApplication>

       

      The error message I get is at the exampleMenu.addItem(item) line and is as follows

       

      Multiple markers at this line

        - 1120: Access of undefined property item

        - 1120: Access of undefined property exampleMenu

       

      Any help is greatly appreciated.

       

      Thanks for reading

        • 1. Re: Native Menu
          leybniz Level 4

          your trouble is due to that line:

           

          //Add the sub item to the root menu
          exampleMenu.addItem(item);

          as you can see it's not obvious for the compiler when exactly this line should be executed??

           

          to avoid error you'd better put your "real code"(not variables declarations) inside some function like this:

           

          private function onInit():void {

               //Add the sub item to the root menu
               exampleMenu.addItem(item);
          }

           

          and link this function to be invoked on application initialization like this:

           

          <mx:WindowedApplication initialize="onInit()" ... />

           

           

          If you feel this message answers your question or helps, please mark it respectively

          • 2. Re: Native Menu
            vertmonkee Level 1

            Excellent, thanks for your help.