首页 > 解决方案 > Kotlin 的后期初始化

问题描述

我写了一个代码来登录和电子邮件和谷歌登录,但我在 Logcat 中得到了错误,所以我读了它,它是由我的代码中的延迟初始化错误引起的,我尝试在溢出中搜索 pproblem 但我没有找到任何东西

kotlin.UninitializedPropertyAccessException:lateinit 属性身份验证尚未初始化原因:kotlin.UninitializedPropertyAccessException:lateinit 属性身份验证尚未在 com.example.preludeprototpe.Activity3Kt.access$getAuth$p(Activity3.kt:1) 在 com.example 处初始化.preludeprototpe.Activity3.checkLoggedInState(Activity3.kt:96) 在 com.example.preludeprototpe.Activity3.onStart(Activity3.kt:48) 在 android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1339) 在 android.app。 Activity.performStart(Activity.java:7167) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2895)

'''

class Activity3 : AppCompatActivity() {
lateinit var auth: FirebaseAuth
private val REQUEST_CODE_SIGN_IN = 0
private val _tag = "Activity3"
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity3)

    btnlogin2activ3google.setOnClickListener {
    val option = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.cliendId))
        .requestEmail()
        .build()
    val sigInClient = GoogleSignIn.getClient(this,option)
    sigInClient.signInIntent.also {
        startActivityForResult(it, REQUEST_CODE_SIGN_IN)
    }

}
 btnloginactiv3.setOnClickListener {
     loginaccount()
 }
}
override fun onStart() {
    super.onStart()
    checkLoggedInState()
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_CODE_SIGN_IN) {
        val account = GoogleSignIn.getSignedInAccountFromIntent(data).result
        account?.let {
            googleaAuthForFirebase(it)
        }
    }
}
private fun googleaAuthForFirebase(account: GoogleSignInAccount) {
    val credential = GoogleAuthProvider.getCredential(account.idToken,null)
    CoroutineScope(Dispatchers.IO).launch {
        try {
        auth.signInWithCredential(credential).await()
            withContext(Dispatchers.Main) {
                Toast.makeText(this@Activity3,"Success login",Toast.LENGTH_LONG).show()
            }
        }catch (e: Exception){
         withContext(Dispatchers.Main) {
             Toast.makeText(this@Activity3,e.message,Toast.LENGTH_LONG).show()
         }
      }
    }
}
private fun loginaccount() {
    val email = etemail.text.toString()
    val password = etpassword.text.toString()
    if(email.isNotEmpty() && password.isNotEmpty()){
        CoroutineScope(Dispatchers.IO).launch {
            try {
                auth.signInWithEmailAndPassword(email,password).await()
                withContext(Dispatchers.Main){
                    checkLoggedInState()
                    Log.d(_tag,"Activity3")
                }
            }catch (e:Exception){
                withContext(Dispatchers.Main){
                    Toast.makeText(this@Activity3,e.message,Toast.LENGTH_LONG).show()
                }
            }
        }
    }
}
private fun checkLoggedInState(){
    if(auth.currentUser == null){
        tvLoggedIn.text =  getString(R.string.loggedin)
    }else{
        tvLoggedIn.text = getString(R.string.loggedin2)
    }
}


} 

标签: androidandroid-studiokotlinkotlin-lateinit

解决方案


你还没有初始化变量。在onCreate中初始化,添加这段代码

auth = FirebaseAuth.getInstance() 

推荐阅读