带有用于 QR 扫描的 zxing 库的 NullPointerException(android app Xamarin Forms)

NullPointerException with zxing library for QR scanning (android app Xamarin Froms)

zxing QR 代码 reader 使用 Java.Lang.NullPointerException 导致崩溃:'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference'下面是我添加的代码。

        {
            try
            {
                Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            catch (Exception)
            {
                return;
            }
        }
// and 
 try
            {
                ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);

                ZXing.Net.Mobile.Forms.Android.Platform.Init();
            }
            catch (Exception)
            {
                return;
            }
 // on the on create 

// button click 

        private async void Button_Clicked(object sender, EventArgs e)
        {
            try
            {
                _scanPage = new ZXingScannerPage();
                if (_scanPage != null)
                {
                    _scanPage.OnScanResult += (result) =>
                    {
                        _scanPage.IsScanning = false;

                        Device.BeginInvokeOnMainThread(() =>
                        {
                            Navigation.PopAsync();
                            DisplayAlert("Scanned Barcode", result.Text, "OK");
                        });
                    };
                }

                await Navigation.PushAsync(_scanPage).ConfigureAwait(true);
            }
            catch (Exception)
            {
                return;
            }
        }

插件详情如下

ZXing.Net.Mobile 2.4.1ZXing.Net.Mobile.Forms 2.4.1

请建议我是否有任何其他我们可以使用的插件

 updated the Initialize scanner code in Main Activity, below are the code 
global::Xamarin.Forms.Forms.Init(this, bundle);
            //-->Initialize scanner
            MobileBarcodeScanner.Initialize(this.Application);

现在工作正常

我在我的 Xamarin Forms - MvvmCross 项目中发现了 Android 的相同错误。我的解决方案是添加

Xamarin.Forms.Forms.Init(this, bundle); //not needed for MvvmCross

在主活动中。现在 MainActivity OnCreate 结果

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    Xamarin.Forms.Forms.Init(this, bundle);  //ZXing needs it
    ZXing.Net.Mobile.Forms.Android.Platform.Init();
}