2012-08-02 33 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

相关问题