2014-03-31 61 views
-1

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

我已经检查了所有与错误有关的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/上面的对话框的错误消失,但它已经停止了再次展示我的警告对话框并显示,我在我的错误日志中已经更新了不同的错误。如果我删除该文件,它会显示警告消息以及错误对话框。

我只是不明白我出错的地方。

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

谢谢你。

+0

看看我的更新问题。我在'assets/www'文件夹中添加了'cordova_plugins.js',但是因为我已经添加了该文件,它已经停止向我显示警报消息。为什么这样 ?如果我删除该文件,它会显示警告消息以及错误对话框。 – GrIsHu

回答

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

0

试试这个

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

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 –

+0

这是一个答案。我已经多次使用它,没有任何问题。 –

+0

你应该把你的代码很少的描述 –