2014-01-15 135 views
0

我在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) 
+0

请张贴异常的详细信息,包括堆栈跟踪。 –

+0

谢谢Stephen,在上面添加了异常的详细信息 –

回答

0

我找不到任何有关设置Zxing库的方法和事件的问题。特别是在MonoTounch上做了相同的设置并且一切正常。因此,我进入源代码,看看scanner.Cancel()方法在内部有什么作用。我发现它只是调用ZxingActivity上的Cancel()static方法,所以我从我的代码中完成了这项工作,并且所有工作都很顺利。 这可能不是最优雅的解决方案,并且我在github上的Zxing.Net.Mobile回购(here)上报告了一个问题,但现在这个工作正常,我希望它可以帮助其他人。我也有一个完整的文章详细介绍了我在MonoDroid上使用这个库的经验,以防有人需要它here。与此一致

ZxingBarcodeScanner.Cancel(); 

所以修复程序替换这条线(从上面的代码)

ZxingActivity.RequestCancel(); 
0

异常的

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); 
     } 
    } 

细节不能因缺乏信​​誉的所以在这里发帖评论。

我们遇到了另一个第三方应用程序超出范围的类似问题。你有没有做过任何级别的线程安全检查?也许你的ZxingBarcodeScanner只能通过创建它的线程来访问,即调用“StartScanSession”的线程。

+0

事件处理程序与上面所见的线程相同 –