尝试执行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(位于资源文件夹并包含在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)。愿有人帮帮我吗?
答案 0 :(得分:1)
你在什么时候打电话给setActualPosition
? deviceready
事件被解雇了吗?如果它未被触发,您将无法访问cordova
个对象。
答案 1 :(得分:0)
我得到了和你一样的错误。这篇文章有一个修复: