Android Kotlin firebase.database.DatabaseException:没有在类

时间:2018-02-21 17:01:39

标签: android firebase firebase-realtime-database kotlin

我遵循了其他SO解决方案的建议: 1.让我的班级Serializableable 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 Rule:

-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)

        }
    }
}

错误日志摘要:

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)

3 个答案:

答案 0 :(得分:0)

解决了,即使在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
    }

}

修改后,其余部分按原样运行。

答案 1 :(得分:0)

如果您在发行apk时遇到问题,则 在您的proguard文件中这样做

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

答案 2 :(得分:0)

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

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