使用 ZXing Xamarin for iOS 的条形码扫描仪应用程序在应用程序类中遇到空引用错误
Posted
技术标签:
【中文标题】使用 ZXing Xamarin for iOS 的条形码扫描仪应用程序在应用程序类中遇到空引用错误【英文标题】:Barcode Scanner App Using ZXing Xamarin for iOS hits null reference error in Application Class 【发布时间】:2019-03-05 20:31:53 【问题描述】:我的公司需要一个基于智能手机的条形码/二维码扫描仪应用程序,它必须在 iPhone 和 android 上运行,但主要是在 iPhone 上运行。我决定试一试 Xamarin Forms,它在 Android 上运行得非常好,但为了让该应用程序甚至出现在 iPhone 上,我花了一些功夫。我终于让它在 iPhone 上打开了,但是当我点击扫描按钮时它会弹出 未处理的异常:
System.NullReferenceException: Object reference not set to an instance of an
object`
在 UIApplication.Main(args, null, "AppDelegate") 上的 Application 类中;
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace QRNatives.ios
public class Application
// This is the main entry point of the application.
static void Main(string[] args)
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
我编程的时间不长,但通常空引用错误似乎很容易追踪。但是,我看不出这个问题实际发生在哪里。我怀疑问题出在 AppDelegate.cs 中,但我并没有真正发现问题。
using Foundation;
using UIKit;
namespace QRNatives.iOS
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
global::Xamarin.Forms.Forms.Init();
global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
只是为了了解更多信息,这是我用于 QrScanningService.cs 的内容,用于将 iOS 链接到共享接口数据。
using System.Threading.Tasks;
using QRNatives.Services;
using Xamarin.Forms;
using ZXing.Mobile;
[assembly: Dependency(typeof(QRNatives.iOS.Services.QrScanningService))]
namespace QRNatives.iOS.Services
class QrScanningService
public class QrCodeScanningService : IQrScanningServies
public async Task<string> ScanAsync()
var scanner = new MobileBarcodeScanner();
var scanResults = await scanner.Scan();
return scanResults.Text;
我已经搜索了许多旧帖子是否出现了这个问题,但我读过的没有一个对这个问题有任何真正的答案。有没有人有任何成功使用 ZXing 和 Xamarin for iOS 的经验?或者有人对我如何追踪错误的实际来源有任何建议吗?我已经坚持了一段时间,所以任何帮助将不胜感激。谢谢。
【问题讨论】:
我实际上发现我的问题发生在按钮单击事件处理程序中。由于某种原因,它没有访问 iPhone 上的相机,因此结果为空。该行...... var scanner = DependencyService.Getvar scanner = DependencyService.Get<IQrScanningServies>(DependencyFetchTarget.NewInstance);
中显示问题所在的相关代码?您是否授予您的应用访问相机的权限?
Jack Hua 我可以为相机添加权限。我用笔记本打开 Info.plist 文件并添加了它......在 IDE 中添加它的选项。之后,它就可以很好地访问相机了。我确实必须重新启动手机并重新输入我的 ID,然后它才能连接到我的 Rest API,但截至今天早上,我已经完全可以在 Android 和 Apple 上运行。
好的,所以您通过在应用程序中添加camera permission
解决了您的问题。此外,您可以在 Visual Studio 中编辑 info.plist
。我将发布一个答案,向您展示如何做到这一点。请接受它,以便我们可以帮助更多有同样问题的人。
对不起,我走了一会儿。会的。
【参考方案1】:
要在iPhone中访问camera
,你应该在info.plist
中添加权限:
<key>NSCameraUsageDescription</key>
<string>Camera acess to scan barcodes</string>
要在 Visual Studio 中编辑 info.plist
:
右键info.plist
并选择open with
,你可以选择Generic PList Editor
或XML editor
(随便你)编辑。
更多权限:
参考:infoplist-reference
【讨论】:
另外,每个人都知道...如果您遇到任何其他权限问题,您需要从手机中获取 UDID 号码并使用该号码将设备添加到您的苹果开发者资料中,然后在您之后'已经完成,您需要将该设备添加到您的开发配置文件中,然后下载该新配置文件并在您的手机连接到的 Mac 计算机上打开它。我花了一段时间才自己弄清楚这一切。以上是关于使用 ZXing Xamarin for iOS 的条形码扫描仪应用程序在应用程序类中遇到空引用错误的主要内容,如果未能解决你的问题,请参考以下文章