Xamarin.forms zxing库用于QR扫描的NullPointerException(android应用程序Xamarin Froms)
zxing QR码读取器在Java.Lang.NullPointerException上出现崩溃:“尝试调用虚拟方法”android.content.res.Resources android.content.Context.getResources()”在空对象引用上“下面是我添加的代码Xamarin.forms zxing库用于QR扫描的NullPointerException(android应用程序Xamarin Froms),xamarin.forms,zxing,barcode-scanner,Xamarin.forms,Zxing,Barcode Scanner,zxing QR码读取器在Java.Lang.NullPointerException上出现崩溃:“尝试调用虚拟方法”android.content.res.Resources android.content.Context.getResources()”在空对象引用上“下面是我添加的代码 { try { Xamarin.Essentials.Platform.OnRequestPermissions
{
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.1和ZXing.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表单-MvvmCross项目中发现了同样的Android错误。我的解决办法是增加
Xamarin.Forms.Forms.Init(this, bundle); //not needed for MvvmCross
在主要活动中。现在main活动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();
}
“你有没有试过在出现时设置IsScan为真?”sermet是的,我也加了这个。我添加了xaml并将isScaning设置为true,但我仍然遇到同样的问题。正如您所看到的代码,我也添加了其他方式。在按钮上单击“我已创建新的”instance@sermet这是Xaml中的代码,我创建了一个新的解决方案,并尝试实现相同的解决方案。它很好用。我的解决方案中可能存在什么问题。我正在使用mvvmcross Java.Lang.NullPointerException:“尝试调用虚拟方法”android.content.res.Resources android.content.Context.getResources()“对空对象引用”我编写了一个小示例,您可以将其标记为正确答案,这将帮助更多有相同问题的人:)。