2012-08-02 31 views
3

我在Android應用程序中使用了PhoneGap插件條碼掃描器。當我將它們附加到按鈕上的onclick事件時,功能window.plugins.barcodeScanner.encodewindow.plugins.barcodeScanner.scan工作得很好。條碼掃描器 - 科爾多瓦插件

然而,當我嘗試在身體/頁的init /頁顯示事件的onload事件執行編碼功能,我得到以下在Eclipse

Uncaught TypeError: Cannot call method 'encode' of undefined at file:///android_asset/www/indexx.html:32 

錯誤謝謝。

回答

0

嘗試調用一次Cordova加載完成後的方法(設備準備事件)

2

您使用哪種版本的phonegap? 1.9.0還是2.0.0?

2.0.0有調用插件的新方法。 上1.9,你可以使用:

window.plugins.barcodeScanner.scan(function(result) { 
    ... 
    .. 
} 

如果您正在使用2.0.0,嘗試初始化插件的方式不同:

window.barcodeScanner = new BarcodeScanner(); 

function scanBarcode() 
{ 
    window.barcodeScanner.scan(function(result) { 
     alert("We got a barcode\n" + 
       "Result: " + result.text + "\n" + 
       "Format: " + result.format); 
    }, function(error) { 
     alert("Error scanning Barcode: " + error); 
    }); 
} 
0

我是新來科爾多瓦,但基於我已經看到它聽起來像@traumalles是正確的。要調用科爾多瓦加載完成後window.plugins.barcodeScanner.encode或window.plugins.barcodeScanner.scan功能,請執行下列操作在您的JavaScript文件:

// Wait for Cordova to load 
document.addEventListener("deviceready", onDeviceReady, false); 

// Cordova is ready 
function onDeviceReady() { 
    // As an example, you now have the device name, Cordova version, etc. available 
    alert('Device Name: ' + device.name); 
    alert('Device Cordova: ' + device.cordova); 
    alert('Device Platform: ' + device.platform); 
    alert('Device UUID: ' + device.uuid); 
    alert('Device Version: ' + device.version); 

    // Now call one of your barcode functions, etc. 
} 

更多信息請參見http://docs.phonegap.com/en/2.0.0/cordova_device_device.md.html#Device

相關問題