【问题标题】:Titanium is not defined when attempting Titanium.UI.currentWindow using titanium appcelerator尝试使用钛应用加速器的 Titanium.UI.currentWindow 时未定义钛
【发布时间】:2013-01-06 01:22:50
【问题描述】:

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

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

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

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

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

在 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);

});

Uncaught ReferenceError: Ti is not defined in the HTML file!!!

【问题讨论】:

  • 您的示例不完整。 .html 文件在哪里?您的 webview 是在哪里创建的?我没有足够的信息来帮助你。
  • 请注意,当您从远程 URL(即 Titanium 应用程序本身之外的 URL)加载 HTML 时,Titanium(和 Ti)命名空间不可用。

标签: javascript html titanium


【解决方案1】:

该代码有许多因素使其无法作为独立的 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();

【讨论】:

  • 感谢 Dawson,目前让任何事情都可以工作将是一个好的开始,因为我时间紧迫。上面的代码有效,但我需要它在从 html 中的 webview 调用时专门工作。如果我在应用程序加载时调用相机,我之前使用的代码可以工作,但我无法调用 app.js 文件中的代码。另外当我尝试:Ti.App.fireEvent。它说 Ti 没有定义
【解决方案2】:

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

【讨论】:

  • 钛和钛是同义词。改变它不会有什么不同。
  • 上次我在 WebView HTML 文件中使用 Titanium API 时,“Ti”不起作用,而是“Titanium”。但它是关于 SDK 版本 1.8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多