Title: Problem with published Adobe Air Application
Tags: xml;flash;actionscript-3;encoding;air
Question: I have an Air Application (Adobe Flash CS4, Adobe AIR 1.1, ActionScript 3.0). I've published it as *.air file and installed it on my computer. It worked fine. But when I tried to use it on the other computer, I found the following problem:


I installed AIR from http://get.adobe.com/ru/air/
I installed my Main.air and launched it.
It can't parse XML file (pattern.xml) correctly.


The code of my app is following:

```public class Main extends MovieClip  {              
    public function Main():void
    {
        this.stop();
        var file:File = File.applicationDirectory.resolvePath("pattern.xml");
        var fileStream = new FileStream();
        fileStream.open(file, FileMode.READ); 
        var str:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
            str=str.substr(1);    
        var panoramaPattern=new XML(str);
        fileStream.close();
    }
}
```

I tried to comment a several commands in Main(). So, code works without

```var panoramaPattern=new XML(str);
```

What is wrong with this command? pattern.xml was included into "Included files".


Here is the accepted answer: I've found the solution.


I've change the encoding of pattern.xml to ANSI
I've change XML loading algorithm to this one


It works!


Here is another answer: I imagine what is happening is that as soon as your swf's main class here (above) is created (right at initialization), the ENTER_FRAME event is binding the event listener to the button, but the button does not technically exist. Your methodology for initializing here is very bad practice but allow me explain how this all works. 

Any time that you have a class that extends a type of DisplayObject, you should ALWAYS create a modified constructor designed to detect the "stage" element, and if it doesn't exist, listen for the ADDED_TO_STAGE event, and then perform your display-object based initializations within the callback. This is because display object based classes are kind of created in a half-assed way. The constructor is called immediately when the class is created/instantiated, but the properties and methods of that class, including children that are display objects (as in this case, buttons etc) are not available until the class has been added to the global "stage" object.

In the case of AIR, your NativeWindow object contains a single instance of "stage" that all children of that NativeWindow inherit. So when you add a MovieClip or a Sprite etc to the stage, the "stage" property of that display object is populated with a reference to the global stage object contained within NativeWindow. So always remember, when it comes to flash the practice with dealing with constructors/initialization of display objects is to delay all functionality to a callback that is processed only when the global "stage" has become available to reference. Below is an example using your code:

```public class Main extends MovieClip  {      

    public function Main():void
    {
        if(stage){
            init();
        }else{
            this.addEventListener(Event.ADDED_TO_STAGE, init);
        }
    }

    //Can be private or public, doesn't matter private is better practice
    private function init(e:Event = null)
    {
        //Notice the function paramter has a default value assigned of null. This is required so we can call this function without args as in the constructor       

        //Also the flag variable is not necessary because this function is called once
        btnDialogCreate.addEventListener(MouseEvent.CLICK,CreateProject);        
    }

    //Also it is generally considered bad practice to put capitals on the start of your function/variable names. Generally only classes are named this way ie: Main.
    public function createProject(e:MouseEvent){
        //You do not need a try/catch statement for simply opening a file browser dialogue. This is a native method you're invoking with a native, predefined default directories inside the VM. Flash is already doing this for you
        var directory:File=File.documentsDirectory;
        directory.browseForDirectory("Directory of project");
    }

}
```

Lastly I would highly recommend watching some of the free video tutorials on this site, as there are a wide range of subjects covered that will teach you much about flash.

http://gotoandlearn.com/


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2011:00:04/02/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xml><flash><actionscript-3><encoding><air>', 'Title': 'Problem with published Adobe Air Application', 'ViewCount': 259, 'id': 5523045, 'user_AboutMe': '<p>.NET developer, software architect at Megatec (http://www.megatec.ru/).</p>\n', 'user_AccountId': 143650.0, 'user_DisplayName': 'Sir Hally', 'user_Reputation': 2268.0, 'user_Views': 243.0, 'user_id': 352130.0}}