尝试执行 phonegap 插件方法时,应用程序会引发此错误:
TypeError 'undefined' is not a function (evaluating 'cordova.exec( ...
应用程序中包含的代码:
Javascript 插件链接(文件 settingswrite.js)
window.SettingsWrite = function(objectData, successCallback, failureCallback) {
var options = {};
for (var key in objectData) {
options[key] = objectData[key];
}
cordova.exec(
successCallback,
failureCallback,
'SettingsWrite',
'set',
new Array(options)
);
};
使用插件的 Javascript 代码(文件 app.js)
function setActualPosition() {
// appMap is an application global object
var map = appMap.getMapEdgesProjection();
window.SettingsWrite([{
x: map.minh,
y: map.maxh
}],
function(r){
alert(r);
},
function(e){
alert("Operation error");
console.log('ERROR: ' + e);
});
}
插件在 config.xml 中声明为:
<plugin name="SettingsWrite" value="es.mycompany.cordova.plugin.SettingsWrite"/>
Javascript 代码执行 index.html(位于 assets 文件夹并包含在 WebView 中):
<!DOCTYPE html>
<html>
<head>
<title>Test Mobile</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<script src="assets/js/cordova-2.0.0.js"></script>
<script src="assets/js/jquery-1.7.2.min.js"></script>
<script src="assets/js/settingswrite.js"></script>
<script src="assets/js/app.js"></script>
</head>
<body>
<div id="divMapContainer"></div>
</body>
</html>
以及插件 Java 类(在 Phonegap 文档中定义):
public class SettingsWrite extends Plugin {
public static final String ACTION = "set";
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
if(ACTION.equals(action)) {
.
.
.
} else {
.
.
.
}
}
该插件在 Android 3.1 设备中运行,并且所有需要的文件都正确包含在项目中(cordova-2.0.0.js 和 cordova-2.0.0.jar)。有人可以帮助我吗?