2012-08-07 41 views
2

我已經在cordova2.0.0中創建了一個應用程序,在cordova的幫助下,我從Java腳本調用本機函數(用c編寫)。cordova 2.0.0給出未捕獲的TypeError:對象0沒有方法'func'

以前我是用的PhoneGap 1.0.0做其中的應用程序工作properly.but當我把它更新到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);被suppossed調用本地Java功能,但它給我上面的錯誤。

回答

0

工作正常,我

測試上的冰激凌三明治和薑餅(您可能需要進行重組去除開關和有你喜歡的任何選項)

嘗試調試您的版本與警報看到,直到其中它走了。

您是否還需要從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> 
相關問題