我在MonoForAndroid中有一個活動,它使用Zxing.Net.Mobile掃描Android上的條形碼。在掃描和返回結果方面,一切正常。但是,當我嘗試處理scanOverlay上的任何事件時,我收到nullReferenceException。我的代碼如下,任何幫助將不勝感激。從異步方法中處理事件
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);
}
}
以上所有代碼工作正常。然而,當我試圖處理上的佈局完成按鈕,我得到的NullReferenceException下面
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)
請張貼異常的詳細信息,包括堆棧跟蹤。 –
謝謝Stephen,在上面添加了異常的詳細信息 –