修改数据库时出现 Firestore ExceptionInInitializerError

Firestore ExceptionInInitializerError at database modification

我尝试将 Firestore 用于我的 Android 应用程序项目,但是当我 运行 这段代码时,它基本上是 Firestore 的文档代码:

protected Integer sendAllToBack () {
    try {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        Map<String, Object> data = new HashMap<>();
        data.put("toto", "tata");
        db.collection("users").add(data);
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }
    return 0;
}

我收到这个错误:

    Process: xxxxxxxxxxxxxx, PID: 14497
    java.lang.ExceptionInInitializerError
        at com.google.firestore.v1.MapValue.newBuilder(com.google.firebase:firebase-firestore@@21.4.3:232)
        at com.google.firebase.firestore.UserDataReader.parseMap(com.google.firebase:firebase-firestore@@21.4.3:290)
        at com.google.firebase.firestore.UserDataReader.parseData(com.google.firebase:firebase-firestore@@21.4.3:251)
        at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.3:232)
        at com.google.firebase.firestore.UserDataReader.parseMergeData(com.google.firebase:firebase-firestore@@21.4.3:87)
        at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.3:165)
        at xxxxxxxxxxxxx.MainActivity.sendAllToBack(MainActivity.java:298)
        at xxxxxxxxxxxxx.MainActivity.onDestroy(MainActivity.java:317)
        at android.app.Activity.performDestroy(Activity.java:8078)
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1353)
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5057)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5101)
        at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.RuntimeException: Unable to get message info for com.google.firestore.v1.MapValue
        at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:62)
        at com.google.protobuf.ManifestSchemaFactory$CompositeMessageInfoFactory.messageInfoFor(ManifestSchemaFactory.java:143)
        at com.google.protobuf.ManifestSchemaFactory.createSchema(ManifestSchemaFactory.java:55)
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:93)
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:107)
        at com.google.protobuf.GeneratedMessageLite.makeImmutable(GeneratedMessageLite.java:171)
        at com.google.firestore.v1.MapValue.<clinit>(com.google.firebase:firebase-firestore@@21.4.3:490)
        at com.google.firestore.v1.MapValue.newBuilder(com.google.firebase:firebase-firestore@@21.4.3:232) 
        at com.google.firebase.firestore.UserDataReader.parseMap(com.google.firebase:firebase-firestore@@21.4.3:290) 
        at com.google.firebase.firestore.UserDataReader.parseData(com.google.firebase:firebase-firestore@@21.4.3:251) 
        at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.3:232) 
        at com.google.firebase.firestore.UserDataReader.parseMergeData(com.google.firebase:firebase-firestore@@21.4.3:87) 
        at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.3:165) 
        at com.bouygues.bysafe.MainActivity.sendAllToBack(MainActivity.java:298) 
        at com.bouygues.bysafe.MainActivity.onDestroy(MainActivity.java:317) 
        at android.app.Activity.performDestroy(Activity.java:8078) 
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1353) 
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5057) 
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5101) 
        at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7710) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
     Caused by: java.lang.UnsupportedOperationException
        at com.google.firestore.v1.MapValue.dynamicMethod(com.google.firebase:firebase-firestore@@21.4.3:482)
        at com.google.protobuf.GeneratedMessageLite.dynamicMethod(GeneratedMessageLite.java:252)
        at com.google.protobuf.GeneratedMessageLite.buildMessageInfo(GeneratedMessageLite.java:280)
        at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:60)
        at com.google.protobuf.ManifestSchemaFactory$CompositeMessageInfoFactory.messageInfoFor(ManifestSchemaFactory.java:143) 
        at com.google.protobuf.ManifestSchemaFactory.createSchema(ManifestSchemaFactory.java:55) 
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:93) 
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:107) 
        at com.google.protobuf.GeneratedMessageLite.makeImmutable(GeneratedMessageLite.java:171) 
        at com.google.firestore.v1.MapValue.<clinit>(com.google.firebase:firebase-firestore@@21.4.3:490) 
        at com.google.firestore.v1.MapValue.newBuilder(com.google.firebase:firebase-firestore@@21.4.3:232) 
        at com.google.firebase.firestore.UserDataReader.parseMap(com.google.firebase:firebase-firestore@@21.4.3:290) 
        at com.google.firebase.firestore.UserDataReader.parseData(com.google.firebase:firebase-firestore@@21.4.3:251) 
        at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.3:232) 
        at com.google.firebase.firestore.UserDataReader.parseMergeData(com.google.firebase:firebase-firestore@@21.4.3:87) 
        at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.3:165) 
        at com.bouygues.bysafe.MainActivity.sendAllToBack(MainActivity.java:298) 
        at com.bouygues.bysafe.MainActivity.onDestroy(MainActivity.java:317) 
        at android.app.Activity.performDestroy(Activity.java:8078) 
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1353) 
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5057) 
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5101) 
        at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7710) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

有人知道我可能做错了什么吗? 我试过 db.collection("users").document("1").set(data, SetOptions.merge()); 这是我一开始想要 运行 的代码,我遇到了确切的问题。

此代码在我的 Android 应用程序的 MainActivity 中,我在 onDestroy 方法中调用它。

这是库版本不匹配造成的。现在实际上可以解决这个问题,但文档有一个实际有效的解决方案。https://firebase.google.com/docs/android/setup#add-sdks

dependencies {
  // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:26.3.0')

  // When using the BoM, you don't specify versions in Firebase library 
  // dependencies

  // Declare the dependency for the Firebase SDK for Google Analytics
  implementation 'com.google.firebase:firebase-analytics'

  // Declare the dependencies for any other desired Firebase products
  // For example, declare the dependencies for Firebase Authentication and 
  // Cloud Firestore
   implementation 'com.google.firebase:firebase-auth'
   implementation 'com.google.firebase:firebase-firestore'
}

要了解有关 BOM 的更多信息,请参阅 Android Bill Of Materials