Android: Namespace undefined when first window heavyweight

You must Login before you can answer or comment on any questions.

Can someone help me on this issue? It works perfectly on iOS but crashes on Android. As soon as you click the button, you get the error "Message: ReferenceError: "namespace" is not defined. (file:///android_asset/Resources/testTwo.js#2)".

This error does not occur if test.js is a light weight window. However we have to make it heavyweight as it is the main window so we can exitOnClose as well as intercept the back button as necessary.

I'm using Android API 2.3 using Titanium SDK 1.7.2.

//app.js 
var namespace = {};
namespace.printout = function(){
    Ti.API.info('namespace.printout');
};
 
Ti.include('/test.js');
var win = namespace.test();
win.open();
//test.js
(function(){
    namespace.test = function(){
        var winTest = Titanium.UI.createWindow({
            title: 'Test',
            backgroundColor: '#fff',
        exitOnClose: true,
            fullscreen: false // making this a heavy weight window
        });
 
        var btnButton = Ti.UI.createButton({
            title: 'Click button',
            width: 150,
            height: 100,
            top: 100
        });
 
        btnButton.addEventListener('click', function(){
            Ti.include('/testTwo.js');
            var winToOpen = namespace.testTwo();
            winToOpen.open();
        });
        winTest.add(btnButton);
 
        namespace.printout();
 
        return winTest;
    };
})();
//testTwo.js
(function(){
    namespace.testTwo = function(){
        var winTestTwo = Titanium.UI.createWindow({
            title: 'Test2',
            backgroundColor: '#fff'
        });
 
        var btnBack = Ti.UI.createButton({
            title: 'back',
            height: 100,
            width: 100,
            top: 50
        });
 
        btnBack.addEventListener('click', function(){
            winTestTwo.close();
        });
 
        winTestTwo.add(btnBack);
        return winTestTwo;
    };
})();

— asked 2 years ago by Vinh Bui
0 Comments

2 Answers

Accepted Answer

ok had a quick look, nice coding, however I think the issue is including the files. The actual path of files work differently on iOS and android, although it is a lot better in 1.7.2 and 1.8 sdk,s.

using / to define the file location is not they way. Try removing this in front of the included file and if this does not work actually put the path in relative to the actual calling file ovation.

hope this helps.

— answered 2 years ago by Trevor Ward
answer permalink
2 Comments
  • also I tend to include the files in app.us to include the anonymous functions at run time. It maybe that the file hasn't loaded when you call the namespace.

    — commented 2 years ago by Trevor Ward

  • Hi Trevor,

    Thanks for replying. I was able to get it to work because of your suggestion. I change the include files location from the click event to somewhere else. Seem like you're right about the files not being loaded yet when I'm calling the namespace.

    Thanks again.

    — commented 2 years ago by Vinh Bui

IMHO, files are loaded asynchronously and that's why you shouldn't include files and use included variables/functions just after that. I use function constructors in included files and I include them on top of the script. Then I can safely use staff from included file in the script.

Your Answer

Think you can help? Login to answer this question!