javascript - cordova-2.0.0.js 不能在 android 上使用 phonegap

标签 javascript android html windows-phone-7 cordova

我是一名新的 PhoneGap 程序员。 navigator.notification.alert() 无法在使用 phonegap 的 Android 上运行。但它适用于 iPhone 和 WP7。我正在 Windows Phone 7 中开发此应用程序。

谢谢你的帮助

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Cordova WP7</title>
    <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title"
        charset="utf-8" />
    <!--<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>-->  
    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript">
        function doAlert() {
            var message = "This is an Alert dialog";
            var title = "Attention!";
            navigator.notification.alert(message, title);
        };       

    </script>
</head>
<body>
    <h1>
        Cordova Tests</h1>
    <div id="info">
         <a href="#" onclick="doAlert()">Click Me</a>  
    </div>
</body>
</html>


最后,我得到了解决方案。

解决方案:

在我们构建 phonegap 代码之前(在 build.phonegap.com)我们需要删除 cordova-2.0.0.js 文件。如果我们删除此 cordova-2.0.0.js,phonegap 代码将在 andriod 上运行。

最佳答案

您必须等待 DeviceReady:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale = 1.0, maximum-scale=1.0, user-scalable=no" />
<script type="text/javascript" charset="utf-8" src="windows/cordova-2.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
    var deviceReady = false;
    function init() {
        document.addEventListener("deviceready", function () {
            deviceReady = true;
        }, false);

        window.setTimeout(function () {
            if (!deviceReady) {
                alert("Error: Phonegap did not initialize.  Demo will not run correctly.");
                console.log("Error: Phonegap did not initialize.  Demo will not run correctly.");
            }
        }, 1000);
    }

    function doAlert() {
        var message = "This is an Alert dialog";
        var title = "Attention!";
        navigator.notification.alert(message, title);
    }
</script>
</head>
<body onLoad="init();">
<h1>Cordova Tests</h1>
<div id="info">
    <button onclick="doAlert();">Click Me</button>
</div>
</body>
</html>

关于javascript - cordova-2.0.0.js 不能在 android 上使用 phonegap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388966/

相关文章:

javascript - 如何将数组的值输入到我的数据库中

android - 无法在 Android 4.4.4 上获得分配

android - 闪屏显示不止一次

html - Bootstrap 移动导航栏被切断

html - 将数据显示到 HTML 表格中

html - flex 的预期生命周期长吗

javascript - 单击事件时无法更新文本

javascript - 在 div 中找到第一个有边距的元素? (目的 : remove margin)

javascript返回映射,键为空字符串

java - Android MapView 类未找到