用于QR扫描的zxing库出现NullPointerException(安卓应用程序Xamarin Froms)



zxing二维码阅读器与Java.Lang.NullPointerException发生崩溃:"试图在null对象引用上调用虚拟方法"android.content.res.Resources android.coontent.Context.getResources((">下面是我添加的代码。

{
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中。现在MainActivity OnCreate results

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

相关内容

最新更新