Android Kotlin firebase.database.DatabaseException:在类上找不到要序列化的属性

Posted

技术标签:

【中文标题】Android Kotlin firebase.database.DatabaseException:在类上找不到要序列化的属性【英文标题】:Android Kotlin firebase.database.DatabaseException: No properties to serialize found on class 【发布时间】:2018-08-01 08:44:48 【问题描述】:

我已遵循其他 SO 解决方案的建议: 1. 使我的课程可序列化 2.有一个Firebase的默认构造函数 3.更新我的proguard规则(不确定这个)

我仍然收到上述错误,有什么想法吗?

package org.sherman.android.stub_firebase.Models

import java.io.Serializable

/**
 * Created by fyi2 on 2/21/18.
 */
class User(displayName:String, email:String, photoUrl:String, userId:String) : Serializable 
    constructor(): this("","","","")




新的类定义:

class User :Serializable 
    private var displayName:String=""
    private var email:String=""
    private  var photoUrl:String=""
    private var userId:String=""

    constructor() 

    constructor(displayName: String, email: String, photoUrl: String, userId: String) 
        this.displayName = displayName
        this.email = email
        this.photoUrl = photoUrl
        this.userId = userId
    


Proguard 规则:

-keepclassmembers class org.sherman.android.stub_firebase.Models.**  *; 

调用它的代码:

fun registerUserToFirebase(email:String, password:String)
    mAuth?.createUserWithEmailAndPassword(email, password)?.addOnCompleteListener(this) task ->
        if (task.isSuccessful)
            mProgressDialog?.dismiss()
            var currentUserFB = task.getResult().user as FirebaseUser
            var user = User("Bobby", currentUserFB!!.email!!, "", currentUserFB!!.uid)
            mUsersDB!!.child(currentUserFB.uid).setValue(user)
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
         else 
            Helper.showAlertDialog("Error!",task.exception?.message!!,this)

        
    

错误日志sn-p:

com.google.firebase.database.DatabaseException: No properties to serialize found on class org.sherman.android.stub_firebase.Models.User
                                                                                       at com.google.android.gms.internal.zzelx.<init>(Unknown Source:799)
                                                                                       at com.google.android.gms.internal.zzelw.zzf(Unknown Source:12)
                                                                                       at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:259)
                                                                                       at com.google.android.gms.internal.zzelw.zzbw(Unknown Source:0)
                                                                                       at com.google.firebase.database.DatabaseReference.zza(Unknown Source:10)
                                                                                       at com.google.firebase.database.DatabaseReference.setValue(Unknown Source:7)
                                                                                       at org.sherman.android.stub_firebase.Activities.RegisterActivity$registerUserToFirebase$1.onComplete(RegisterActivity.kt:59)

【问题讨论】:

【参考方案1】:

已解决,即使在 Kotlin 中,变量似乎也必须明确公开。

import java.io.Serializable

/**
 * Created by fyi2 on 2/21/18.
 */
class User :Serializable 
    public var displayName:String=""
    public var email:String=""
    public  var photoUrl:String=""
    public var userId:String=""

    constructor() 

    constructor(displayName: String, email: String, photoUrl: String, userId: String) 
        this.displayName = displayName
        this.email = email
        this.photoUrl = photoUrl
        this.userId = userId
    


修改后,其余部分照常工作。

【讨论】:

【参考方案2】:

如果您在发布 apk 时遇到问题,那么 在你的 proguard 文件中这样做

    -keepattributes Signature
    -keepclassmembers class com.yourcompany.models.** 
      *;
    

【讨论】:

【参考方案3】:

您可以轻松使用数据类,它已经带有一个空的构造函数等。

@IgnoreExtraProperties
data class User(
    var displayName: String? = null,
    var email: String? = null,
    var photoUrl : String? = null,
    var userId: String? = null
)

【讨论】:

以上是关于Android Kotlin firebase.database.DatabaseException:在类上找不到要序列化的属性的主要内容,如果未能解决你的问题,请参考以下文章

Android@Kotlin 在Android studio 中配置Kotlin

Android@Kotlin 在Android studio 中配置Kotlin

Android@Kotlin 在Android studio 中配置Kotlin

Android为什么要从Java改用Kotlin

Android为什么要从Java改用Kotlin

FAQ | 使用 Kotlin 进行 Android 开发