【问题标题】:Getting error in Firebase cloud database while creating database创建数据库时在 Firebase 云数据库中出现错误
【发布时间】:2019-07-24 21:28:06
【问题描述】:

我正在创建如 Firebase 文档中所示的数据库。但我收到数组错误。但我没有使用任何数组。

这是我的代码:

private void addNewContact() {

    if (!TextUtils.isEmpty(name.getText())){
        if (!TextUtils.isEmpty(email.getText())) {
            if (!TextUtils.isEmpty(phone.getText())){

                Map<String, Object> newContact = new HashMap<>();
                newContact.put(NAME_KEY, name.getText());
                newContact.put(EMAIL_KEY, email.getText());
                newContact.put(PHONE_KEY, phone.getText());

                db.collection("PhoneBook").document("Contacts").set(newContact)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(FirebaseCloudActivity.this, "User Registered",
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(FirebaseCloudActivity.this, "ERROR" + e.toString(),
                                        Toast.LENGTH_SHORT).show();
                                Log.d("TAG", e.toString());
                            }
                });

            }
        }
    }
} 

出现以下错误:-

java.lang.IllegalArgumentException:无法序列化对象。不支持序列化数组,请改用列表

【问题讨论】:

    标签: android google-cloud-firestore cloud


    【解决方案1】:

    Firebase 尝试序列化 getter,而 Firebase 尝试序列化 .getText() 的结果。

    请将您的地图更改为:

    Map<String, Object> newContact = new HashMap<>();
    newContact.put(NAME_KEY, name.getText().toString());
    newContact.put(EMAIL_KEY, email.getText().toString());
    newContact.put(PHONE_KEY, phone.getText().toString());
    

    【讨论】:

    • 非常感谢 Ashish。你节省了我的时间。
    【解决方案2】:

    变化:

    newContact.put(NAME_KEY, name.getText());
    

    收件人:

    newContact.put(NAME_KEY, name.getText().toString());
    

    Firebase 尝试序列化 getter,这与 firebase 尝试序列化 .getText() 的结果类似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多