12 Replies Latest reply on Mar 4, 2009 7:49 AM by JosePenaV

    Help with changing states

    m_hartnett Level 3
      This is my second post on this same topic and I have not been able to figure out what is wrong.
      So any help would be appreciated (Adobe do you have any ideas).

      Here is some very simple code that just switches between States.
      There is an issue when switching between Company and Advertise.

      It seems that switching between states that are based on the base state is no problem.
      But when I switch between states that are NOT based on the base state then I get an error.

      If you click through a few times going between the Company and Advertise states you will see.

      This seems like a simple issue but maybe I am not understanding how to architect flex applications.
      I would appreciate any help and let me know If I am off base on what I am doing.


      have been trying to figure out what I am doing wrong
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
      backgroundColor="#fbe68e" width="100%" height="100%"
      borderStyle="outset" borderColor="#004dbf" cornerRadius="20"
      horizontalAlign="center" verticalAlign="middle" currentState="homeView">

      <mx:State name="homeView">
      <mx:SetStyle target="{lbHome}" name="color" value="#cc0000"/>
      <mx:AddChild relativeTo="{canvas6}" position="lastChild">
      <mx:Panel x="117" y="10" width="250" height="200" layout="absolute" id="homePanel" title="Home">

      <mx:State name="ourCompanyView">
      <mx:SetStyle target="{lbOurCompany}" name="color" value="#cc0000"/>
      <mx:AddChild relativeTo="{canvas6}" position="lastChild">
      <mx:Panel x="108" y="10" width="323" height="228" layout="absolute" id="ourCompanyPanel" title="Our Company">
      <mx:State name="compAboutUsView" basedOn="ourCompanyView">

      <mx:State name="adWithUsView">
      <mx:SetStyle target="{lbAdWithUs}" name="color" value="#cc0000"/>
      <mx:AddChild relativeTo="{canvas6}" position="lastChild">
      <mx:Panel x="95.5" y="10" width="325" height="228" layout="absolute" id="adWithUsPanel" title="adWithUs">
      <mx:State name="adDesignView" basedOn="adWithUsView">

      <mx:Canvas cornerRadius="9" borderStyle="solid" borderColor="#000000" backgroundColor="#9ba2c1" width="443" height="250" id="canvas6" verticalScrollPolicy="off" horizontalScrollPolicy="off">
      <mx:LinkButton x="10" y="10" height="16" label="Home" id="lbHome" click="currentState='homeView'" fontSize="10"/>
      <mx:LinkButton x="10" y="42" height="16" label="Advertise" id="lbAdWithUs" click="currentState='adDesignView'" fontSize="10"/>
      <mx:LinkButton x="10" y="25" height="16" label="Company" id="lbOurCompany" click="currentState='compAboutUsView'" fontSize="10"/>

        • 1. Re: Help with changing states
          FlightGuy Level 1
          What error do you get? This looks ok - clearly the addDesignView and compAboutUsView don't do anything, but I wouldn't expect an error. The actual error might give some hints, though.

          • 2. Re: Help with changing states
            m_hartnett Level 3
            Thanks for looking at this Tim.

            The error I get is:
            Error: Cannot add a child that is already parented.
            at mx.states::AddChild/apply()
            at mx.core::UIComponent/::applyState()
            at mx.core::UIComponent/::applyState()
            at mx.core::UIComponent/::commitCurrentState()
            at mx.core::UIComponent/setCurrentState()
            at mx.core::UIComponent/set currentState()
            at index1/__lbOurCompany_click()

            You can see that this code is extremely simple. Just switching between states.

            The problem seems to happen if a state is based on something other than base state. My states are:

            base state

            I can switch between views that are based on the base view and no errors.

            When I switch between the two states adDesignView (linkButton Advertise) and compAboutUsView (linkButton Company) the first time I can see that the color change for the linkbutton does not happen. I assume this is where the initial error occurs.

            If I keep clicking on these two linkbuttons I will get the above error.

            I can move the code from the ourCompanyView into its related state of compAboutUsView and do the same with the Advertise states and the error goes away.

            Also if I make all the states based on the base state the problem disappears. However this forces me to create duplicate objects that could be contained in a parent state.

            I understand that in changing states that flex will first change to the base state and then change to the desired state.
            Maybe I dont understand how to change between states that are not based on the base state.

            • 3. Re: Help with changing states
              m_hartnett Level 3
              I also see that if I switch states by first changing to the views parent and then to the view it will work with no errors.

              For example, the following works:

              The ourCompanyView is based on the base state.

              Is this the way I am supposed to change views that are not based on the base state?
              • 4. Re: Help with changing states
                FlightGuy Level 1
                Very weird - you're not attempting anything that shouldn't work. I tried your example and get the same problem. The error happens because it's attempting to add back a panel that it never removed. You can see that moving from adDesignView to compAboutUsView fails to remove the panel added by adDesignView - which it should do.

                This is a flex bug I would have to say. If you're feeling really energetic you could step through the UIComponent code in the debugger and see why it's not removing the added components.
                • 5. Re: Help with changing states
                  FlightGuy Level 1
                  OK, I was feeling energetic. The error is at line 6530 of UIComponent.as. The loop looks like this:

                  while (firstBaseStates[firstBaseStates.length] ==
                  commonBase = firstBaseStates.pop();

                  if (!firstBaseStates.length || !secondBaseStates.length)

                  Its purpose is to work back through the arrays of parents looking for the last common base. Problem is that the last element of an array, X, is

                  X[X.length - 1]



                  So if someone at Adobe could fix it you'd be good to go. Meanwhile, set the state back to base first, and then to your desired state. If you have transitions this could be more challenging, but there are ways 'round it.

                  • 6. Re: Help with changing states
                    m_hartnett Level 3
                    Thanks for taking a look at that.

                    I suspected it might be a bug but it seemed to big and obvious of an error to have been missed by Adobe QA.

                    I am just getting into transitions now and may hit you up for some tips later. I can see how this flex bug will add a challenge.

                    So far Flex kicks *** on the old web/html/javascript/xml/xslt etc... world and gives me back the freedom to build a rich client server application.

                    Thanks for your help.
                    • 7. Re: Help with changing states
                      FlightGuy Level 1
                      If you do get to using state transitions, note that if you use the setCurrentState method to change states, you can specify in an optional second parameter whether or not you wish to run you transition effects.

                      Flex is great - nothing you can do in any presentation technology that you can't do in flex/flash, provided you stay in 2D.

                      • 8. Re: Help with changing states
                        netsesame Level 1
                        I got such an error just like you.

                        Now since it is a bug, then I don't need to try to find some solutions.
                        • 9. Re: Help with changing states
                          netsesame Level 1
                          JUST AS FlightGuy had pointed that,
                          the findCommonBaseState method in the UIComponent.as has such bug:
                          while (firstBaseStates[firstBaseStates.length]) == secondBaseStates[secondBaseStates.length])....
                          Obviously, the code is over bounds of Array, which is a funny bug, is it?
                          So, if there exits more than one deepth state level, the findCommonBaseState will return a WRONG value. That is!

                          Really hope this BUG can be resolved in the next patch OR version.
                          • 10. Re: Help with changing states
                            Sander Lissenburg
                            I just ran into the same problem, I have solved it now by setting the state back to te base state as Flyguy suggested, but like Flyguy I see a problem in this when I want to use transitions in the future. I hope adobe will fix the problem before I will build in te transitions. But 3 months have already past since the first user figured out the problem....
                            • 11. Re: Help with changing states
                              JabbyPandaUA Level 3
                              I confirm that findCommonBaseState returns a WRONG value in Flex 2.0.1 SDK when switching between states that are dependent on other states by relying on "basedOn" property
                              • 12. Re: Help with changing states
                                I thnk that the problem is associate to an Object that is being added to a parent, which has been added before. so in the problem throws this error, you need before adding this component remove the component from the parent to which you are adding and after that add the component, I get the same problem and figure out this a solution