【问题标题】:Event handling from inside an Async method异步方法内部的事件处理
【发布时间】: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


【解决方案1】:

由于缺乏声誉,无法发表评论,因此在此处发布。

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

【讨论】:

    【解决方案2】:

    我在设置Zxing 库的方法和事件的方式上没有发现任何问题。尤其是在MonoTounch 上进行了相同的设置并且一切正常之后。因此,我进入源代码查看scanner.Cancel() 方法在内部做了什么。我发现它只是调用了ZxingActivity 上的Cancel() static 方法,所以我从我的代码中做到了这一点,而且似乎一切正常。 这可能不是最优雅的解决方案,我已经在 github (here) 上的 Zxing.Net.Mobile repo 上报告了一个问题,但现在这可行,我希望它也可以帮助其他人。我还有一个完整的帖子,详细介绍了我在MonoDroid 上使用这个库的经验,以防有人需要它here

    所以修复替换了这一行(来自上面的代码):

    ZxingBarcodeScanner.Cancel();
    

    用这一行:

    ZxingActivity.RequestCancel();  
    

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2014-01-30
      • 2019-08-04
      • 2011-09-11
      • 2011-08-03
      • 1970-01-01
      • 2014-04-02
      • 2018-02-13
      • 2012-08-23
      相关资源
      最近更新 更多