5 Replies Latest reply on Oct 5, 2009 5:48 AM by timo888

    Why does my menu index = -1  ?

    timo888 Level 1

      Source for three items is attached. There are two reusable mxml components (StandardSearchDialog, ProximitySearchDialog) and my main application (HelloWWW.mxm).

       

      There are three choices on the main menu:

       

      Standard

      Proximity

      Phrase

       

      Selecting "standard" injects StandardSearchDialog into a panel.  Selecting "Proximity" or "Phrase" injects ProximitySearchDialog into that panel. I remove all children from the panel before injecting the component.

       

      The menu and itemClick handler work as expected, but only for so long, and I'm not sure why it breaks after I have interacted with the app for a while. But sooner or later the MenuBar itemClick event starts sending an index of -1 to the handler.  I've tried to figure out what combination of actions is causing this, but I'm stumped.

       

      I'm just starting to learn Flex, so any help would be much appreciated.

       

      Thanks

        • 1. Re: Why does my menu index = -1  ?
          Flex harUI Adobe Employee

          Could you be clicking on disabled menu items?

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Why does my menu index = -1  ?
            timo888 Level 1

            Thanks for the suggestion, but, no, all of the menu-items are enabled.

             

            I've rescoped the Dialogs so they're not created afresh with each menu choice, and also in the  mxml definitions, I've rescoped the ContextMenu.  This has improved the situation dramatically but has not completely cured it. Every so often, maybe once every 50 times, the menu item click returns -1 as the index.

             

            This makes me think there's a memory/garbage-collection issue.

            • 3. Re: Why does my menu index = -1  ?
              timo888 Level 1

              I've taken a different approach to toggling my custom dialogs (mxml components), now using <mx:states>. Now the problem, where the menu click event returns -1 as the selected item index, occurs even sooner than it did when I was taking the AddChild/RemoveAllChildren approach in ActionScript. However, if I wait a few seconds and try the menu again, it begins to work again.

               

              So I suppose it's pretty easy to get ahead of garbage collection when swapping mxml components in and out, regardless of whether it's done in ActionScript or with <mx:states>.  But invoking the garbage collector right before the state is changed seems to ameliorate the problem:

               

               

              private function itemClickHandler(e:MenuEvent):void {

                  System.gc();
                  if ( e.index == 0) {

                       currentState="StandardState";
                 }
                  if ( (e.index==1) || (e.index==2)) {

                      currentState="ProximityState";
                  }
                }        

              <mx:states>
                      <mx:State name="StandardState">
                          <mx:AddChild
                              relativeTo="{SearchDialogPanel}"
                              position="firstChild"
                              creationPolicy="all"
                              >
                               <ns1:StandardSearchDialog />
                     
                          </mx:AddChild>
                      </mx:State>   
                     
                     <mx:State name="ProximityState">
                          <mx:AddChild
                              relativeTo="{SearchDialogPanel}"
                              position="firstChild"
                              creationPolicy="all"
                              >
                               <ns1:ProximitySearchDialog />
                     
                          </mx:AddChild>
                      </mx:State>   
                     
                  </mx:states>

              • 4. Re: Why does my menu index = -1  ?
                Flex harUI Adobe Employee

                One way to debug this is to add code to the event handler to detect the -1 condition and put a breakpoint on that code.  Then when you hit it, use the "step into" when you're on the last line of the handler.  That should let the debugger end up at the line after the code in Menu that dispatched the event, and then you can examine properties of the menu at the time the event was dispatched.

                 

                From looking at the code, somehow selectedIndex==-1.  Are you using a custom menu itemrenderer?

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                1 person found this helpful
                • 5. Re: Why does my menu index = -1  ?
                  timo888 Level 1

                  I think the problem may be in the Flex class that toggles the radiobutton on the menu. There, selectedIndex = -1, and index = selectedIndex. I've submitted a bug report.

                  Thanks