Titanium错误:未捕获TypeError:无法调用未定义的方法'add'

时间:2013-12-06 09:38:28

标签: android function titanium undefined

我有一个错误,只在android上: 我有这样的代码:

var selfWin;

var blackScreen;
var actInd;

var Login = require('ui/common/Login');
var myLogin;

var HomeView = require('ui/common/Home');
var homeView;

function ApplicationWindow() {

    var selfWin = Ti.UI.createWindow({
        backgroundColor:'#ffffff',
        navBarHidden:true,
        exitOnClose:true
    });

    blackScreen = Ti.UI.createLabel({
        backgroundColor:'#000000',
        opacity:0.40,
        top:0,
        height:'100%',
        width:'100%',
        zIndex:100
    });
    actInd = Ti.UI.createActivityIndicator({
        width:50,
        height:50,
        zIndex:101
    });

    selfWin.add(blackScreen);
    selfWin.add(actInd);
    Ti.App.HideBlackScreen();

    Ti.App.GoToFirstView();

    return selfWin;
}

Ti.App.ShowBlackScreen = function ShowBlackScreen() {
    blackScreen.show();
    actInd.show();
};
Ti.App.HideBlackScreen = function HideBlackScreen() {
    blackScreen.hide();
    actInd.hide();
};

Ti.App.GoToFirstView = function GoToFirstView() {
    myLogin = new Login();
    selfWin.add(myLogin);
    if (homeView) {
        selfWin.remove(homeView);
    }
};

Ti.App.GoToHome = function GoToHome() {
    homeView = new HomeView();
    selfWin.add(homeView);
    selfWin.remove(myLogin);
};

//make constructor function the public component interface
module.exports = ApplicationWindow;

并且在selfWin.add(myLogin)中出现此错误;在GoToFirstView()函数内部,仅在android上。 有人可以帮帮我吗? 非常感谢

1 个答案:

答案 0 :(得分:0)

尝试让你的selfWin对象像下面那样全局。因为它是私有变量,它不存在于Application function()之外。有两种方法可以实现这一目标。

1

   var selfWin;
    function ApplicationWindow() {
           selfWin = Ti.UI.createWindow({
            backgroundColor : '#ffffff',
            navBarHidden : true,
            exitOnClose : true
        });
    }

2.使用下面的功能通过窗口对象。

Ti.App.GoToFirstView(selfWin);
Ti.App.GoToFirstView = function GoToFirstView(selfWin) {
myLogin = new Login();
selfWin.add(myLogin);
if (homeView) {
    selfWin.remove(homeView);
}
};

最诚挚的问候, Nitin Chavda