【发布时间】:2014-01-15 02:21:10
【问题描述】:
我在 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
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);
}
}
以下异常的详细信息
未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例 在 Leopard.Mobile.Screens.QVgaPortrait.MainScreen.HandleDoneButtonOnZxingScanLayout (Leopard.Mobile.Business.Event.ScanSessionEventArgs) [0x0001e] in ...\MainScreen.cs:178 在 Leopard.Mobile.Screens.QVgaPortrait.MainScreen/c__DisplayClass8.b__6 (object,System.EventArgs) [0x00000] in ...\MainScreen.cs:156 在 Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000d] 在 /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 在 Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00011] 在 /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 在(包装器动态方法)object.49957671-33c0-4b79-8c3b-36f419ebfaaa(intptr,intptr,intptr)【问题讨论】:
-
请发布异常详细信息,包括堆栈跟踪。
-
感谢斯蒂芬,添加了上面异常的详细信息
标签: c# android asynchronous xamarin.android zxing