kotlin.TypeCastException:无法将null强制转换为非null类型kotlin.String

时间:2020-07-19 02:21:32

标签: string kotlin

我正在制作数据快照,但出现错误

val passwordFromDB = datasnapshot.child(completePhoneNumber).child("password").getValue<String>(String::class.java) as String
if (passwordFromDB.equals(userPassword)) {
    et_Password.error = null
    //get user data from datasnapshot
    val usernameFromDB = datasnapshot.child(completePhoneNumber).child("name").getValue<String>(String::class.java) as String
    val fullnameFromDB = datasnapshot.child(completePhoneNumber).child("fullname").getValue<String>(String::class.java) as String
    val emailFromDB = datasnapshot.child(completePhoneNumber).child("email").getValue<String>(String::class.java) as String
    val phoneFromDB = datasnapshot.child(completePhoneNumber).child("phone").getValue<String>(String::class.java) as String

    //session manager
    val sessionmanager =
            SessionManager(this@LoginActivity) as SessionManager
    // not impacted to user layout
    sessionmanager.createloginSession(
            usernameFromDB,
            fullnameFromDB,
            emailFromDB,
            phoneFromDB,
            passwordFromDB
    )
}

错误:

2020-07-19 09:14:57.129 21850-21850 / com.example.configuration E / AndroidRuntime:致命异常:main 流程:com.example.configuration,PID:21850 kotlin.TypeCastException:无法将null强制转换为非null类型kotlin.String 在com.example.configuration.login.LoginActivity $ DoLogin $ 1.onDataChange(LoginActivity.kt:139)

1 个答案:

答案 0 :(得分:0)

我认为问题出在(代码的)第3行。 et_Password.errorNon-nullable String,因此无法设置为null(就像您在代码中所做的那样)。尝试通过添加类似{p>的et_Password.error使Nullable String成为?

var error: String? = null