Reputation: 1123
When trying to execute a phonegap plugin method, the application raises this error:
TypeError 'undefined' is not a function (evaluating 'cordova.exec( ...
Code included in app:
Javascript plugin link (file 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 code to make use of plugin (file 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);
});
}
Plugin is declared in config.xml as:
<plugin name="SettingsWrite" value="es.mycompany.cordova.plugin.SettingsWrite"/>
Javascript code is executed index.html (located at assets folder and included into 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>
And the plugin Java class (as defined in Phonegap docs):
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 {
.
.
.
}
}
The plugin runs in Android 3.1 device and all needed files are correctly included to the project(cordova-2.0.0.js and cordova-2.0.0.jar). May someone help me?
Upvotes: 1
Views: 2008
Reputation: 5267
At what point are you calling setActualPosition
? Has the deviceready
event fired? If it has not fired, you will not have access to the cordova
objects.
Upvotes: 1
Reputation: 257
I was getting the same error as you. There is a fix in this post:
datePicker plugin not working in Phonegap 2.0
Upvotes: 0