【问题标题】:Late init in Kotlin [closed]Kotlin 的后期初始化 [关闭]
【发布时间】:2020-10-06 12:37:05
【问题描述】:

我写了一个代码来登录和电子邮件和谷歌登录,但我在 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)
    }
}


} 

【问题讨论】:

    标签: android android-studio kotlin kotlin-lateinit


    【解决方案1】:

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

    auth = FirebaseAuth.getInstance() 
    

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2020-12-25
      • 2021-08-29
      相关资源
      最近更新 更多