Xamarin iOS集成Firebase Crashlytics启动时触发空引用崩溃求助
Xamarin iOS集成Firebase Crashlytics启动崩溃:空引用异常
我正在开发一款Xamarin跨平台iOS和Android应用,已完成Firebase Crashlytics的安装流程,Android端集成后正常工作,但iOS端添加Crashlytics上报代码后,应用启动时崩溃,报错:Object reference not set to an instance of an object.。
调用栈信息如下:
[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at app.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x00007] in C:\Users\user\source\repos\app\app.iOS\AppDelegate.cs:28 at (wrapper managed-to-native) UIKit.UIApplication.xamarin_UIApplicationMain(int,string[],intptr,intptr,intptr&) at UIKit.UIApplication.UIApplicationMain (System.Int32 argc, System.String[] argv, System.IntPtr principalClassName, System.IntPtr delegateClassName) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.2.0.5/src/Xamarin.iOS/UIKit/UIApplication.cs:57 at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.2.0.5/src/Xamarin.iOS/UIKit/UIApplication.cs:92 at diarioDeTeruelIOS.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\user\source\repos\app\app.iOS\Main.cs:14 at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 ' The app has been terminated.
目前确认Firebase.iOS.Core相关功能无异常,仅调用Firebase.iOS.Crashlytics类/方法时出现问题。开发环境为Visual Studio 2022,基于.NET Standard 2.1,调试设备为实体iPhone 7。
AppDelegate.cs代码如下:
namespace app.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) { Initializer.Initialize(); global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); Firebase.Core.App.Configure(); Crashlytics.SharedInstance.Init(); Crashlytics.SharedInstance.SetCrashlyticsCollectionEnabled(true); Crashlytics.SharedInstance.SendUnsentReports(); /* * */ return base.FinishedLaunching(app, options); } } }
可能的解决方案
- 确认
Firebase.iOS.CrashlyticsNuGet包版本与Firebase.iOS.Core完全兼容,避免版本不匹配导致的实例化失败 - 检查
GoogleService-Info.plist是否已正确添加到iOS项目,且Build Action设置为BundleResource - 调整初始化顺序,将Firebase Core配置移至Xamarin Forms初始化之前:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { Firebase.Core.App.Configure(); Initializer.Initialize(); global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); Crashlytics.SharedInstance.Init(); Crashlytics.SharedInstance.SetCrashlyticsCollectionEnabled(true); Crashlytics.SharedInstance.SendUnsentReports(); return base.FinishedLaunching(app, options); } - 清理iOS项目的bin/obj目录,重新生成整个解决方案
- 卸载设备上的现有应用,重新部署调试
- 检查Info.plist中是否配置了Crashlytics所需的网络权限(iOS默认允许HTTP/HTTPS请求,但可确认是否存在限制)
内容的提问来源于stack exchange,提问作者Lorem
相关产品推荐
相关产品推荐

