I have an activity in MonoForAndroid that makes use of Zxing.Net.Mobile for scanning barcodes on Android. Everything works fine in terms of scanning and returnning results. However, when I try to handle any events on the scanOverlay, I get nullReferenceException. My code is below, any help would be appreciated.
public async void StartScanSession(ScanSessionEventArgs e)
{
EnsureLoadingZxingOverlay(e);
EnsureStartingZxingBarcodeScanner();
var zxingOptions = MobileBarcodeScanningOptions.Default;
var result = await ZxingBarcodeScanner.Scan(zxingOptions);
HandleScanResult(result, e);
}
private void HandleScanResult(ZXing.Result result, ScanSessionEventArgs e)
{
if (result != null && e.OnFinishCallBack != null)
{
var scanResult = new ScanResult { ShouldStopScanning = false, BarcodeText = result.Text, ScanTime = result.Timestamp, BarcodeFormat = result.BarcodeFormat.ToString(), RawBytes = result.RawBytes };
e.OnFinishCallBack(scanResult);
}
}
private void EnsureLoadingZxingOverlay(ScanSessionEventArgs e)
{
if (ZxingOverlay == null)
{
ZxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.scan_custom_layout, null);
ScanLayoutFlashButton = ZxingOverlay.FindViewById<Button>(Resource.Id.ScanLayoutFlashButton);
ScanLayoutDoneButton = ZxingOverlay.FindViewById<Button>(Resource.Id.ScanLayoutDoneButton);
UnhookZxingLayoutButtons();
ScanLayoutFlashButton.Click += (sender, args) => ZxingBarcodeScanner.ToggleTorch();
ScanLayoutDoneButton.Click += (sender, args) => HandleDoneButtonOnZxingScanLayout(e);
}
}
All this code above is working fine. However, when I try to handle Done button on the layout, I get the NullReferenceException
private void HandleDoneButtonOnZxingScanLayout(ScanSessionEventArgs e)
{
var result = new ScanResult { ShouldStopScanning = true };
if (e.OnFinishCallBack != null && ZxingBarcodeScanner != null)
{
// at this line below, ZxingBarcodeScanner is null,
// but I am sure I have initiated before wiring the event
// I am guessing it is something to do with the context of the async method??
ZxingBarcodeScanner.Cancel();
e.OnFinishCallBack(result);
}
}
Details of the exception below
UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Leopard.Mobile.Screens.QVgaPortrait.MainScreen.HandleDoneButtonOnZxingScanLayout (Leopard.Mobile.Business.Event.ScanSessionEventArgs) [0x0001e] in ...\MainScreen.cs:178 at Leopard.Mobile.Screens.QVgaPortrait.MainScreen/c__DisplayClass8.b__6 (object,System.EventArgs) [0x00000] in ...\MainScreen.cs:156 at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Views.View.cs:1615 at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Views.View.cs:1582 at (wrapper dynamic-method) object.49957671-33c0-4b79-8c3b-36f419ebfaaa (intptr,intptr,intptr)