2

我在cordova2.0.0中创建了一个应用程序,我在cordova的帮助下从java脚本调用本机函数(用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);应该调用本机 java 函数,但它给了我上述错误。

4

1 回答 1

0

适合我

在冰淇淋三明治和 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>
于 2012-10-09T09:04:20.997 回答