使用钛金属加速器尝试Titanium.UI.currentWindow时未定义Titanium

时间:2013-01-06 01:22:51

标签: javascript html5 titanium

当我在设备上运行应用程序时,我没有得到任何错误(根本没有任何事情发生),因为没有调试控制台,但是当我在浏览器中运行应用程序时,我得到“Titanium未定义”

我需要包含一个js文件吗?

我从这里得到了相机代码: http://www.mindfiresolutions.com/Capture-Image-with-device-camera-in-IPhoneAndroid-application-using-Titanium-1912.php

我从webview的html文件中调用它。

我从零开始创建了一个新项目,我得到了同样的错误。这太令人沮丧了:

in html:

<!doctype html>
<html lang="en">
    <head>

        <meta name="viewport" content="width=device-width;initial-scale=1.0 maximum-scale=1.0; user scalable=0;">

        <title>Notes</title>

        <script language="JavaScript" type="text/javascript">
            function play(locid) {
                Ti.App.fireEvent('play', {
                    locid : locid
                });
            }
        </script>

    </head>
    <body>

        <a id="Home" onclick ="play(125)" title = "" > hello </a>

    </body>

</html>
app.js中的

Ti.App.addEventListener('play', function(e) 
{ 
    alert(e.locid);

});

未捕获的ReferenceError:未在HTML文件中定义Ti!

2 个答案:

答案 0 :(得分:0)

该代码有许多阻止它作为独立app.js工作的东西。我想解决它带来的一些问题,然后我将直接解决app.js,以便你可以继续前进。

首先,“Ti.UI.currentWindow”是在这种情况下获取对窗口的引用的方式:

在“app.js”文件中,您有:

var win = Ti.UI.createWindow({
    url: 'win.js'
});
win.open();

在“win.js”中,你有:

var win = Ti.UI.currentWindow;
win.add(Ti.UI.createLabel({
    text: 'Hello, world!', textAlign: 'center',
    color: '#000'
}));

但是,构建这样的应用程序不再是推荐的方法了。如果你刚刚开始新的,那就开始吧 - 用Alloy。 http://docs.appcelerator.com/titanium/3.0/#!/guide/Alloy_Quick_Start

如果您真正关心的是让示例正常工作,则下面是更新后的代码:

//Define the current window
var myWin = Ti.UI.createWindow({
    backgroundColor: 'white'
});

//Define the button to activate camera
var cameraBtn = Ti.UI.createButton({
    title: 'Click to activate Camera',
    top: 10 //Set the button position on the screen
});

cameraBtn.addEventListener('click', function () {

    //Activate camera
    Ti.Media.showCamera({
        success: function (event) {

            //Holds the captured image
            var capturedImg = event.media;

            // Condition to check the selected media
            if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {

                //Define an image view with captured image
                var imgView = Ti.UI.createImageView({
                    left: 10,
                    width: 250,
                    height: 250,
                    image: capturedImg   //Set captured image
                });

                //Add the image to window for displaying
                myWin.add(imgView);
            }
        },
        cancel: function () {
            //While cancellation of the process
        },
        error: function (error) {
            // If any error occurs during the process

        }
    });
});

myWin.add(cameraBtn);
myWin.open();

答案 1 :(得分:0)

在WebView HTML代码中使用“Titanium”而不是“Ti”。

相关问题