Late init in Kotlin

232 Views Asked by At

I write a code to login and email and google sign in but I got and error in Logcat,so I read it and it caused by late init proverty in my code,I try searching pproblem in overflow but I did not find anything

kotlin.UninitializedPropertyAccessException: lateinit property auth has not been initialized Caused by: kotlin.UninitializedPropertyAccessException: lateinit property auth has not been initialized at com.example.preludeprototpe.Activity3Kt.access$getAuth$p(Activity3.kt:1) at com.example.preludeprototpe.Activity3.checkLoggedInState(Activity3.kt:96) at com.example.preludeprototpe.Activity3.onStart(Activity3.kt:48) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1339) at android.app.Activity.performStart(Activity.java:7167) at 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)
    }
}


} 
1

There are 1 best solutions below

0
On

You haven't init the variable. Init it in onCreate, add this code

auth = FirebaseAuth.getInstance()