android com.google.firebase.database.DatabaseException:将数据保存到rtdbfirebase时,在java.lang.StringBuilder类上未找到要序列化的属性

bpsygsoo  于 2023-04-28  发布在  Android
关注(0)|答案(1)|浏览(98)

我试图将数据保存到实时数据库的firebase。但我得到这个错误

com.google.firebase.database.DatabaseException: No properties to serialize found on class java.lang.StringBuilder
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:548)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:330)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:167)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:142)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(CustomClassMapper.java:61)
at com.google.firebase.database.DatabaseReference.setValueInternal(DatabaseReference.java:282)
at com.google.firebase.database.DatabaseReference.setValue(DatabaseReference.java:159)

我以前的项目,我使用相同的代码,但现在我得到这个错误。
下面是我用来保存数据的函数代码

private fun saveDetails() {

    val userDataRef: DatabaseReference =
        FirebaseDatabase.getInstance().reference.child("UsersData").child(currentUserId)

    val userMap = HashMap<String, Any>()
    userMap["name"] = name
    userMap["email"] = email
    userMap["imageUrl"] = profileImageUrl
    userMap["phoneNumber"] = number

        userDataRef.setValue(userMap).addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Toast.makeText(
                    baseContext,
                    "$name has successfully registered with email id $email.",
                    Toast.LENGTH_SHORT
                ).show()
                startActivity(Intent(this, MainActivity::class.java))
            } else {
                Toast.makeText(baseContext, task.result.toString(), Toast.LENGTH_SHORT).show()
                Log.e("Task  Unsuccessful", task.result.toString())
            }
        }
    }
hs1ihplo

hs1ihplo1#

Firebase似乎在序列化userMap中的某个值时遇到了问题。在这种情况下,很可能您的某个值的类型是java.lang.StringBuilder而不是java.lang.String
要解决这个问题,请确保您放入userMap的所有值都是String类型。如果其中任何一个是StringBuilder,则应调用toString()方法将其转换为String。

private fun saveDetails() {
    val userDataRef: DatabaseReference =
        FirebaseDatabase.getInstance().reference.child("UsersData").child(currentUserId)

    val userMap = HashMap<String, Any>()
    userMap["name"] = name.toString()
    userMap["email"] = email.toString()
    userMap["imageUrl"] = profileImageUrl.toString()
    userMap["phoneNumber"] = number.toString()

    userDataRef.setValue(userMap).addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Toast.makeText(
                baseContext,
                "$name has successfully registered with email id $email.",
                Toast.LENGTH_SHORT
            ).show()
            startActivity(Intent(this, MainActivity::class.java))
        } else {
            Toast.makeText(baseContext, task.result.toString(), Toast.LENGTH_SHORT).show()
            Log.e("Task  Unsuccessful", task.result.toString())
        }
    }
}

相关问题