【问题标题】:"plugins is not defined" in PhoneGap ApplicationPhoneGap 应用程序中的“未定义插件”
【发布时间】:2012-09-16 14:50:56
【问题描述】:

我目前正在尝试使用 PhoneGap LocalNotification 插件, 将 Android 4.1 与 Cordova 2.0.0 和 Sketcha(UI 库)一起使用。

2012 年 9 月 20 日更新 #2:我使用了 Bozzzi 的代码并进行了一些修复:

  1. 添加 }) 到 localnotification.js 的末尾(语法错误)。
  2. cordova.define("cordova/plugin/LocalNotification", function(require, exports, module) 更改为: cordova.define("cordova/plugin/LocalNotification", function(require, exports, module), 因此模块名称将与此功能匹配(在未找到模块之前):

    window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");

  3. 从此改变:

    如果(!window.plugins){ window.plugins = {}; } 否则如果(!window.plugins.LocalNotification){ window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification"); }

到这里:

if (!window.plugins) {
    window.plugins = {};
}

if (!window.plugins.LocalNotification) {
    window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
}

在此更改之前,如果没有找到 window.plugins,则会创建它,但不会创建 window.plugins.LocalNotification。

在所有这些修复之后,我收到了这个错误:

> 09-20 01:22:27.355: D/CordovaLog(8297):
> file:///android_asset/www/index.html: Line 21 : PAPA AMERICANO ready
> 09-20 01:22:27.360: I/Web Console(8297): PAPA AMERICANO ready at
> file:///android_asset/www/index.html:21 09-20 01:22:27.370:
> D/CordovaLog(8297): Uncaught TypeError: Cannot call method 'add' of
> undefined 09-20 01:22:27.375: D/CordovaLog(8297):
> file:///android_asset/www/index.html: Line 35 : Uncaught TypeError:
> Cannot call method 'add' of undefined 09-20 01:22:27.375: E/Web
> Console(8297): Uncaught TypeError: Cannot call method 'add' of
> undefined at file:///android_asset/www/index.html:35 09-20
> 01:22:29.185: D/DroidGap(8297): onMessage(spinner,stop) 09-20
> 01:22:30.255: E/SKIA(8297): FimgApiStretch:stretch failed 09-20
> 01:22:41.755: E/SKIA(8297): FimgApiStretch:stretch failed 09-20
> 01:22:52.565: D/CordovaWebView(8297): >>> loadUrlNow() 09-20
> 01:22:52.565: D/webkit(8297): Firewall not null 09-20 01:22:52.565:
> D/webkit(8297): euler: isUrlBlocked = false

由于某种原因,cordova.require("cordova/plugin/LocalNotification") 没有设置 window.plugins.LocalNotification 值并且它一直处于未定义状态。 这是我更新的 index.html(更新 #2):

<!DOCTYPE HTML>
<html manifest="" lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>AndroidSencha</title>
    <script id="microloader" type="text/javascript" src="cordova-2.0.0.js"></script>
    <script id="microloader" type="text/javascript" src="LocalNotification.js"></script>
    <script id="microloader" type="text/javascript">

    function onDeviceReady() {
        if (!window.plugins) {
            window.plugins = {};
        } else if (!window.plugins.LocalNotification) {
            window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
        }
    }

    window.document.addEventListener("deviceready", appReady, false);

    function appReady() {
        console.log("PAPA AMERICANO ready");

        var d = new Date();
        d = d.getTime() + 2 * 1000; //60 seconds from now
        d = new Date(d);

        if (!window.plugins) {
            window.plugins = {};
        }

        if (!window.plugins.LocalNotification) {
            window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
        }

        window.plugins.localNotification.add({
            date: d,
            message: 'This is an Android alarm using the statusbar',
            id: 123
        });
    }

    </script>
    <style type="text/css">
         /**
         * Example of an initial loading indicator.
         * It is recommended to keep this as minimal as possible to provide instant feedback
         * while other resources are still being loaded for the first time
         */
        html, body {
            height: 100%;
            background-color: #1985D0
        }

    </style>

    <!-- The line below must be kept intact for Sencha Command to build your application -->
    <script id="microloader" type="text/javascript" src="sdk/microloader/development.js"></script>
        <style type="text/css">
.loadingText{
color: white;
font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
text-align: center;
font-size: 20px;
padding-top: 10%;
}
</style>
</head>
<body>
    <div id="appLoadingIndicator">
    <div class="loadingText"><div style="margin-bottom: 10px;">Loading, Please wait..</div>
    <div><img src="resources\images\smileloading.gif"></div></div>
    </div>

</body>
</html>

这是我修复后的 localnotificaiton.js(更新 #2):

cordova.define("cordova/plugin/LocalNotification", function(require, exports, module) {      

var exec = require("cordova/exec");
var LocalNotification = function () {};

LocalNotification.prototype.add = function(options) {
            var defaults = {
                date : new Date(),
                message : '',
                ticker : '',
                repeatDaily : false,
                id : ""
            };
        if (options.date) {
                options.date = (options.date.getMonth()) + "/" + (options.date.getDate()) + "/"
                        + (options.date.getFullYear()) + "/" + (options.date.getHours()) + "/"
                        + (options.date.getMinutes());
            }

            for ( var key in defaults) {
                if (typeof options[key] !== "undefined")
                    defaults[key] = options[key];
            }

            cordova.exec(null, null, "LocalNotification", "add",  new Array(defaults));
};

LocalNotification.prototype.cancel = function(notificationId) {
    cordova.exec(null, null, 'LocalNotification', 'cancel', new Array({
        id : notificationId
    }));
};

LocalNotification.prototype.cancelAll = function() {
    cordova.exec(null, null, 'LocalNotification', 'cancelAll', new Array());
};


var LocalNotification = new LocalNotification();
module.exports = LocalNotification
});

更新 #1:我已经将插件添加到 plugins.xml 文件中:

<plugins>
...
  <plugin name="LocalNotification" value="com.phonegap.plugin.localnotification.LocalNotification"/>
...
</plugins>

我已经完成了here 中提到的步骤,并替换了here 中提到的损坏的 java 表达式。

不幸的是,当我运行应用程序时出现以下错误(在模拟器和设备中):

09-16 16:46:16.330:E/Web 控制台(27875):未捕获的 ReferenceError: 插件未在文件中定义:///android_asset/www/index.html:74

This guy was facing the same problem 但我在 index.html 中引用了 cordova javascript 文件,但它仍然无法正常工作。

这就是我的包资源管理器的样子(您可能会发现缺少一些东西): 我不确定我是否应该拥有 plugins.xml 或 config.xml 并在其中包含插件, 我有两个以防万一

提前致谢!

【问题讨论】:

    标签: cordova phonegap-plugins


    【解决方案1】:

    我更改了 LocalNotification.js,以便您可以复制代码。它适用于cordova 2.0(经过测试!)

    cordova.define("cordova/plugin/js/LocalNotification", function(require, exports, module) {      
    
    var exec = require("cordova/exec");
    var LocalNotification = function () {};
    
    LocalNotification.prototype.add = function(options) {
                var defaults = {
                    date : new Date(),
                    message : '',
                    ticker : '',
                    repeatDaily : false,
                    id : ""
                };
            if (options.date) {
                    options.date = (options.date.getMonth()) + "/" + (options.date.getDate()) + "/"
                            + (options.date.getFullYear()) + "/" + (options.date.getHours()) + "/"
                            + (options.date.getMinutes());
                }
    
                for ( var key in defaults) {
                    if (typeof options[key] !== "undefined")
                        defaults[key] = options[key];
                }
    
                cordova.exec(null, null, "LocalNotification", "add",  new Array(defaults));
    };
    
    LocalNotification.prototype.cancel = function(notificationId) {
        cordova.exec(null, null, 'LocalNotification', 'cancel', new Array({
            id : notificationId
        }));
    };
    
    LocalNotification.prototype.cancelAll = function() {
        cordova.exec(null, null, 'LocalNotification', 'cancelAll', new Array());
    };
    
    
    var LocalNotification = new LocalNotification();
    module.exports = LocalNotification;
    

    });

    现在你可以调用通知了。与:

        datum = '2012-09-20 15:51:00';  
    tt1 = datum.split(/[- :]/);
        dd = new Date(tt1[0], tt1[1]-1, tt1[2], tt1[3], tt1[4], tt1[5]);
    
    window.plugins.LocalNotification.add({ date: dd, message: "Some message", ticker : "Some ticker", repeatDaily : false, id: 1234 });
    

    【讨论】:

    • 别忘了:`function onDeviceReady() { if (!window.plugins) { window.plugins = {}; } else if (!window.plugins.LocalNotification) { window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification"); } } `
    • 首先,非常感谢您的帮助。我已经尝试了您的代码,并为此苦苦挣扎,并修复了一些问题以使其正常工作。看看主帖,我已经提到了我所做的步骤,看看它们是好是坏。奇怪的是它在我遇到问题时为你工作:(
    • 您可以在 [link]bozzzi.mine.nu/Aviran 查看源代码如果您发现代码有困难,我会为您上传 Eclipse 项目。您可以从屏幕截图中看到,当我点击“试试我”时,我会在模拟器中激活通知...
    • 下班回来看源码。但是,如果您可以上传 eclipse 项目以防我无法使其工作,那就太好了。谢谢
    • 我对这个有疑问。 “cordova/plugin/js/LocalNotification”是什么意思? LocalNotification 的位置?是那个js文件吗?
    【解决方案2】:

    插件的 .js 尚未更新到 2.0.0 规范。看看我最近发表的一篇关于如何编写符合 2.0.0 规范的插件的博文。

    http://simonmacdonald.blogspot.ca/2012/08/so-you-wanna-write-phonegap-200-android.html

    【讨论】:

    • 我没有时间看这个,我明天再看。感谢分享!
    【解决方案3】:

    检查 cordova 2.2 的最新插件更新 LocalNotification 2.2

    【讨论】:

      【解决方案4】:

      使用 window.plugin 而不是单独使用插件,看看会发生什么。

      【讨论】:

      • 我已经用 window.plugin 替换了插件,我得到这个错误:09-16 19:00:26.700: D/CordovaLog(524): Uncaught TypeError: Cannot read property 'localNotification' of未定义 09-16 19:00:26.700:D/CordovaLog(524):file:///android_asset/www/index.html:第 74 行:未捕获的类型错误:无法读取未定义的属性 'localNotification' 09-16 19:00 :26.700:E/Web 控制台(524):未捕获类型错误:无法读取文件中未定义的属性“localNotification”:///android_asset/www/index.html:74
      • 澄清一下,它的“插件”不是“插件”。
      • 你是否在 plugins.xml 中添加了 localnotification 插件?
      • 是的,我已经将插件添加到 plugins.xml 文件中(我已经更新了 plugins.xml 和 config.xml)。我已经用插件配置更新了我的帖子。
      • 这个问题是否反映了您的 index.html 的当前状态?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多