我在 cordova2.0.0 中创建了一个应用程序,我在 cordova 的帮助下从 java 脚本调用 native 函数(用 c 编写)。
之前我使用的是 phonegap 1.0.0,应用程序在其中运行正常。但是当我将它更新到 cordova 2.0.0 时,出现错误
08-07 15:24:12.527: I/Web Console(855): JSCallback Error: Request failed. at file:///android_asset/www/cordova-2.0.0.js:3698
08-07 15:22:27.815: E/Web Console(855): Uncaught TypeError: Object 0 has no method 'func' at file:///android_asset/www/file.js:359
谁能帮我解决这个问题。
我的 .html 文件的代码:
<!DOCTYPE html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova2.0.0.js"></script>
<script type="text/javascript" charset="utf-8" src="find.js"></script>
<script type="text/javascript">
function js_find(fthisval){
ws_find(socket,js_found);
}
function js_found(retval){
alert(retval);
}
function onBodyLoad(){
document.addEventListener("deviceready",onDeviceReady,false);
}
function onDeviceReady()
{
document.getElementById('d1').style.visibility = "visible";
}
</script>
</head>
<body onload="onBodyLoad()">
<div id="d1" style="visibility:hidden">
<button id="b1" onclick="js_find("ram")">Find it plz</button><br />
</div>
</body>
在 find.js 中:
var Find = function() {};
Find.prototype.func = function(funcname,funcdata) {
return cordova.exec(
null,
null,
'findPlugin',
funcname,
funcdata);
};
window.find = new Find();
function ws_find(name, param, callback){
if(undefined == callback){
var dataArray = [param, param, name];
}
else{
var dataArray = [callback, callback, name, param];
}
alert("Calling plugin function find with \r\n"+dataArray);
return window.find.func("find",dataArray);
}
行 return window.find.func("find",dataArray);
应该调用 native java 函数,但它给了我上述错误。
最佳答案
对我来说很好用
在 Ice-Cream Sandwich 和 GingerBread 上进行了测试(您可能需要重组以移除开关并选择您喜欢的任何选项)
尝试使用警报调试您的版本以查看它的去向。
你还需要从 cordova.exec
返回吗?
<!DOCTYPE HTML>
<html>
<head>
<title>Cordova</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
function callPlugin(type){
switch(type){
case 1:
//Func Ptr
window.echo = function(str, callback) {
cordova.exec(callback, function(err) { callback('Nothing to echo.'); }, "Echo", "echo", [str]);
};
//Func Call
window.echo("echome", function(echoValue){alert(echoValue);} );
break;
case 2:
var MyClass = function (){}; //Prototype
MyClass.prototype.echo = function(str, callback) {
cordova.exec(callback, function(err) { callback('Nothing to echo.'); }, "Echo", "echo", [str]);
};
window.echo = new MyClass();
window.echo.echo("echome2", function(echoValue){alert(echoValue);} );
//OR
//window.echo = new MyClass().echo; //Add Function to Window Object
//window.echo("echome1", function(echoValue){alert(echoValue);} ); //Call Plugin
break;
}
}
</script>
</head>
<body>
<h1>Plugin Test</h1>
<button onclick="callPlugin(1)">Call Plugin by Cordova 2.0.0 Method</button><br />
<button onclick="callPlugin(2)">Call Plugin by Cordova 1.0.0 Method</button><br />
</body>
</html>
关于javascript - cordova 2.0.0 给出未捕获的类型错误 : Object 0 has no method 'func' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11844250/