当我在 Firebase Cloud Messages 发出通知后尝试打开应用程序时,应用程序关闭并出现以下错误:
07-09 12:24:22.404 Samsung SM-A510F Info 15570 MonoDroid System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.NotSupportedException: AddDefaultTypes should be called before creating a Realm instance with the default schema. If you see this error, please report it to help@realm.io.
at Realms.Schema.RealmSchema.AddDefaultTypes (System.Collections.Generic.IEnumerable`1[T] types) [0x00029] in <62b40c9633874f2cb89599c718233d7b>:0
at RealmModuleInitializer.Initialize () [0x00000] in <24d2a06b332042f2a7af7631479a79db>:0
at <Module>..cctor () [0x00000] in <24d2a06b332042f2a7af7631479a79db>:0
--- End of inner exception stack trace ---
at Autofac.Module.Configure (Autofac.Core.IComponentRegistry componentRegistry) [0x0001a] in <57e4c9c21cbb498aacce99da9e049074>:0
at Autofac.ContainerBuilder.Build (Autofac.Core.IComponentRegistry componentRegistry, System.Boolean excludeDefaultModules) [0x0004b] in <57e4c9c21cbb498aacce99da9e049074>:0
at Autofac.ContainerBuilder.Build (Autofac.Builder.ContainerBuildOptions options) [0x0002e] in <57e4c9c21cbb498aacce99da9e049074>:0
at Retail360.Kernel.Build () [0x00071] in <3e794d04561a416fae55be827fe69b19>:0
at Retail360.App..ctor () [0x00015] in <3e794d04561a416fae55be827fe69b19>:0
at Retail360.Droid.MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x0003d] in <9722cafe2466481e865733de664af995>:0
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <2530aa2e412d4754af3ae7e1a8c80b6d>:0
at (wrapper dynamic-method) System.Object.12(intptr,intptr,intptr)
从后台 Worker 发送的 Firebase 消息(使用 AndroidX.Work) 这并不总是发生,但我找不到模式。 我没有在 Autofac 中注册 Realm 的实例,其他注册的类不包含在构造函数中实例化 Realm 实例。
这个错误是什么意思,我做错了什么?
环境:
- 手机三星 A5 Android 7.0 或 OnePlus 6T Android 9。
- Xamarin 表单 3.6.0.539721
- Realm 4.0.1
- Autofac 4.9.2
- Xamarin.Firebase.Messaging 60.1142.1
最佳答案
我通过在创建作业和 worker 之前在 MainActivity 中添加数据库初始化来修复错误。
关于android - 如何修复 "AddDefaultTypes should be called before creating a Realm instance with the default schema.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57035602/