警报未显示在Phonegap中

时间:2014-03-31 06:17:40

标签: android cordova phonegap-plugins phonegap-build

我正在尝试学习PhoneGap并开始创建简单的演示。我刚刚编写了一个代码来显示页面上的警报。但它根本没有显示警报并显示Unable to open asset URL: file:///android_asset/www/cordova_plugins.json的错误。

我已经查看了与错误相关的SO的所有答案,并且尝试了但没有人帮助过我。

更新:

以下是我的错误:

  03-31 03:18:42.040: E/AndroidProtocolHandler(3710): Unable to open asset URL: file:///android_asset/www/cordova_plugins.json
03-31 03:18:42.220: I/chromium(3710): [INFO:CONSOLE(15)] "Uncaught SyntaxError: Unexpected token :", source: file:///android_asset/www/cordova_plugins.js (15)
03-31 03:18:42.240: I/chromium(3710): [INFO:CONSOLE(56)] "Uncaught module cordova/plugin_list not found", source: file:///android_asset/www/cordova.js (56)
03-31 03:18:45.810: I/chromium(3710): [INFO:CONSOLE(19)] "Uncaught TypeError: Cannot call method 'alert' of undefined", source: file:///android_asset/www/index.html (19)
03-31 03:18:47.010: I/chromium(3710): [INFO:CONSOLE(6725)] "deviceready has not fired after 5 seconds.", source: file:///android_asset/www/cordova.js (6725)
03-31 03:18:47.030: I/chromium(3710): [INFO:CONSOLE(6718)] "Channel not fired: onPluginsReady", source: file:///android_asset/www/cordova.js (6718)
03-31 03:18:47.040: I/chromium(3710): [INFO:CONSOLE(6718)] "Channel not fired: onCordovaReady", source: file:///android_asset/www/cordova.js (6718)
03-31 03:18:47.040: I/chromium(3710): [INFO:CONSOLE(6718)] "Channel not fired: onCordovaConnectionReady", source: file:///android_asset/www/cordova.js (6718)
03-31 03:18:49.950: I/chromium(3710): [INFO:CONSOLE(19)] "Uncaught TypeError: Cannot call method 'alert' of undefined", source: file:///android_asset/www/index.html (19)

这是我的html文件:

  <!DOCTYPE HTML>
 <html>
 <head>
 <title> My First PhoneGap App </title>
 <meta name="viewport" content="width=device-width, user-scalable=no"/>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

                        var showMessageBox = function() {

                             navigator.notification.alert("Hello World of PhoneGap");

                        }

                        function init(){
                                document.addEventListener("deviceready", showMessageBox, true);     
                               navigator.notification.alert("Hello World of PhoneGap");          

                        }

</script>

 </head>

 <body>
 <h1> Welcome To First PhoneGap Demo </h1>
  <p><a href="#" onclick="init(); return false;">Show Alert</a></p>
  </body>
 </html>

的Config.xml

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns= "http://www.w3.org/ns/widgets"
        xmlns:gap ="http://phonegap.com/ns/1.0"
        id = "com.testdemo.myfirstapp"
        versionCode="1.0"
        version="1.0.0" 
        >

    <name>My First Application </name>
    <description>This is my first demo application</description>

     <feature name="http://api.phonegap.com/1.0/device" />

     <preference name="permissions"       value="none"/>

       <plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
       <plugin name="Notification" value="org.apache.cordova.Notification"/>
</widget>

enter image description here

由于我在cordova_plugins.js文件夹中添加了asset/www/文件,上面对话框的错误消失了,但它已经停止再次向我显示警告对话框,并显示我已更新的不同错误我的错误日志。如果我删除该文件,它会显示警告消息以及错误对话框。

我只是不明白我哪里出错了。

请帮我解决这个问题。任何帮助将不胜感激。

谢谢你。

2 个答案:

答案 0 :(得分:0)

您添加了两个插件。你在cordova_plugins.js中声明了吗?

像这样的东西。

cordova.define('cordova/plugin_list', function(require, exports, module) {
    module.exports = [{
        "file": "plugins/org.apache.cordova.dialogs/www/notification.js",
        "id": "org.apache.cordova.dialogs.notification",
        "merges": ["navigator.notification"]
    }, {
        "file": "plugins/org.apache.cordova.network-information/www/network.js",
        "id": "org.apache.cordova.network-information.network",
        "clobbers": ["navigator.connection", "navigator.network.connection"]
    }];
    module.exports.metadata = // TOP OF METADATA 
    {
        "org.apache.cordova.device": "0.2.8",
        "org.apache.cordova.network-information": "0.2.7"
    }
});

请检查一下。 http://docs.phonegap.com/en/3.0.0/plugin_ref_spec.md.html

答案 1 :(得分:0)

尝试这个

navigator.notification.alert('Your Alert Message', null, 'Alert', 'OK');