是什么原因导致此AndroidRuntimeException?

时间:2018-08-02 08:18:37

标签: c# android xamarin.forms xamarin.android

我需要一些帮助来理解为什么抛出此异常。例外是:

  

Android.Util.AndroidRuntimeException:只有创建视图层次结构的原始线程才能触摸其视图。

Link to hastebin with full exception

我使用ZXing.Net.Mobile.Forms进行条形码扫描,并使用Rg.Plugins.Popup显示弹出窗口。我相信其中之一会导致异常。

异常似乎是随机抛出的。该应用程序可以在99%的时间内正常运行。

ScannerPage.xaml

<zxing:ZXingScannerView x:Name="ScannerView"
    Result="{Binding ScanResult, Mode=OneWayToSource}"
    ScanResultCommand="{Binding ScanResultCommand}"
    IsScanning="{Binding IsScanning}"
    IsAnalyzing="{Binding IsAnalyzing}" />
<zxing:ZXingDefaultOverlay x:Name="ScannerOverlay"
    BottomText="Scanning will happen automatically"
    ShowFlashButton="False"/>

ScannerPageViewModel.cs(去除了不相关的部分)

[PropertyChanged.AddINotifyPropertyChangedInterface]
internal class ScannerPageViewModel : INavigatedAware
{
    public ScannerPageViewModel(IScannerService scannerService, IUserDialogs dialogs, IPopupNavigation popups, IScreenService screen)
    {
        ScanResultCommand = new Command(ProcessBarcode);
    }

    public ICommand ScanResultCommand { get; }

    /// <summary>
    /// Show info dialog box with ticket info.
    /// </summary>
    private async Task ShowInfoScanResult(string message)
    {
        var popup = new PopupViews.InfoScanResult(Popups, message);
        popup.Disappearing += (se, ev) => IsAnalyzing = true;
        await Popups.PushAsync(popup);
    }

    private void ProcessBarcode()
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            if (ScanResult != null && !string.IsNullOrEmpty(ScanResult.Text))
            {
                // Disable the scanner after one barcode is found.
                IsAnalyzing = false;

                var source = new CancellationTokenSource();

                // Show loading animation if scanning takes >1 second.
                var t = Task.Run(async () =>
                {
                    await Task.Delay(1000, source.Token);
                    Device.BeginInvokeOnMainThread(ShowLoading);
                });

                // Call the web service to process the barcode.
                var scanResponse = await ScannerService.ScanBarcode(ScanResult.Text, ScanningSession, SelectedScanAction);

                if (scanResponse.IsSuccessful)
                {
                    var scanResult = scanResponse.Data;
                    if (scanResult.Success)
                    {
                        var json = scanResult.BarcodeInfo;
                        var message = ParseJsonBarcodeInfo(json);

                        if (SelectedScanAction == ScanAction.Information)
                            await ShowInfoScanResult(message);
                        else
                            await ShowOkScanResult(message);
                    }
                    else
                    {
                        await ShowErrorScanResult(scanResult.FaultDescription);
                    }
                }
                else
                {
                    ShowScanRequestError(scanResponse.ErrorMessage);
                }

                source.Cancel(); // Cancel loading animation timer.
                HideLoading();
                Screen.SetFullscreen();
                source.Dispose();
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

我已经为我创建了Android的依赖服务,它可以正常工作,请检查以下代码。

PCL项目

public interface IBarcodeScanner
{
    Task<string> ScanAsync();
}

然后在Android项目中

[assembly: Dependency(typeof(BarcodeScanner))]
namespace CodeMashScanner.Droid.Helpers
{
    public class BarcodeScanner : IBarcodeScanner
    {
        public async Task<string> ScanAsync()
        {
            var scanner = new ZXing.Mobile.MobileBarcodeScanner(Forms.Context;
            var scanResults = await scanner.Scan();

            return scanResults.Text;
        }
    }
}