tags:

views:

804

answers:

3

How can i call MXML components inside my ActionScript Class.

// filename.mxml

<mx:Canvas x="181" y="180" width="333">
     <mx:Button styleName="LoginButton" id="loginButton" click="checkLogin();" x="160" y="261"/>
     <mx:TextInput styleName="loginTextInput" id="username" x="160" y="161"/>
     <mx:TextInput styleName="loginTextInput" id="password" displayAsPassword="true" x="160" y="191"/>

 </mx:Canvas>

</mx:Canvas>

// main.as [ Class File ]

var obj:filename= new filename();

     private function label_link(evt:TextEvent):void {

                obj.currentState = "defaultindex";
          obj.username.text = "";
          obj.password.text = "";
                }

Objective

I need to access the username and password fields of MXML in my ActionScript Class.

+2  A: 

Say you had an MXML file in folder src/com/stackoverflow/coolstuff/MyCoolBox.MXML

import com.stackoverflow.coolstuff.MyCoolBox;
var coolBox:MyCoolBox = new MyCoolBox();
// Do something
Christopher W. Allen-Poole
TypeError: Error #1009: Cannot access a property or method of a null object reference.I get this error when i try to set my textbox clear... coolBox.TextElementID = "";
Would you post the MXML class so that I can have a better idea what to look for?
Christopher W. Allen-Poole
Added as per your request, please do help me
The component has not been initialised so it's controls are all null. You need to subscribe to CREATION_COMPLETE via coolBox.addEventListener.
Richard Szalay
While I'll agree that it's a good idea to listen to CREATION_COMPLETE, I'm going to disagree with the prognosis. Any time that an MXML file is created, the "id" properties are the equivalent of creating a public variable.These two are more or less equivalent:public var myButton:RadioButton;public function constructor(){ myButton = new RadioButton(); addChild( myButton );}and<mx:RadioButton id="myButton" />
Christopher W. Allen-Poole
The id properties are compiled as public variables, but they are not instanced until a call to createChildren via initialize. For subclasses of Container, the creation occurs in accordance to it's creationPolicy. http://www.adobe.com/livedocs/flex/3/html/layoutperformance_05.html
Richard Szalay
A: 

Ok, series of questions:

  1. Where is the other tag? Is it possible that somehow the wrong file is being used?
  2. What line has the failure? Is it obj.password or could it be obj.currentState (a scoping issue)?
  3. What happens when you trace( obj );
    1. What happens when you trace( getQualifiedClassName( obj ) )?( remember to import flash.utils.getQualifiedClassName first ).
      The second trace should be exactly the same as the import statement.
    2. When you trace( obj.numChildren ), do you get the number you expect?
    3. When you trace( obj.getChildAt( 0 ) ), do you get a Canvas?
      1. When you trace( Canvas( obj.getChildAt( 0 ) ).getChildAt( 0 ) ), do you get another Canvas?
      2. When you trace( Object( obj.getChildAt( 0 ) ).getChildAt( 0 ).getChildren() );
  4. On a bit of a different line of thought -- is this an MXML Flex project or an Actionscript Flex project? (I've seen that change how MXML is handled).

These are the debugging commands I would start with. If you get unexpected results, trace everything before setting the state. Let me know if you're still stuck.

Christopher W. Allen-Poole
A: 

Have you added obj to your main.as via addChild? What is the base class of main.as? You can test this by calling obj.initialize() in your main.as constructor.

Children are not created until initialize is called, usually via addChild when the component is added to it's parent. However, your main.as is not an MXML file which means it is not automatically calling these methods.

Richard Szalay