【问题标题】:NullPointerException with zxing library for QR scanning (android app Xamarin Froms)带有用于 QR 扫描的 zxing 库的 NullPointerException(Android 应用 Xamarin Forms)
【发布时间】:2019-11-20 14:45:13
【问题描述】:

zxing QR 码阅读器导致崩溃 Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on an 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

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

【问题讨论】:

  • 您是否尝试在 OnAppearing 上设置 IsScanning 为真?
  • @sermet 是的,我也添加了。我在 xaml 中添加并将 isScaning 设置为 true 仍然遇到同样的问题。如您所见,我也以其他方式添加了代码。在按钮上单击我已经创建了新实例
  • @sermet 这是 Xaml 中的代码
  • 我已经创建了一个新的解决方案并尝试实现它。它工作正常。我的解决方案可能是什么问题。我正在使用 mvvmcross Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on an null object reference'

标签: xamarin.forms zxing barcode-scanner


【解决方案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);

现在可以正常使用了

【讨论】:

  • 您可以将其标记为正确答案,这将帮助更多有相同问题的人:)。
【解决方案2】:

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

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

在 MainActivity 中。现在 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    相关资源
    最近更新 更多