android - 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)
}
}
}
解决方案
你还没有初始化变量。在onCreate中初始化,添加这段代码
auth = FirebaseAuth.getInstance()
推荐阅读
- python - 使用python提取数据
- javascript - 如何从另一个函数向连接到 Node.js 套接字服务器的客户端写入数据?
- tensorflow - 如何将 TensorFlow 数据集输入到 traning_x、training_y、testing_x、testing_y 到 Keras API?
- bash - 使用 OpenWRT 路由器的自动 SSH 到 AWS EC2 服务器
- python - 获取“过时元素引用”时尝试循环浏览页面以抓取多个页面
- javascript - 为什么 useEffect 不改变段落?
- android - Android Opencv 无法拼接所有图像
- python - Pong - 当任一玩家达到分数阈值时崩溃而不是重置游戏
- java - java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException 与 ANT web 项目
- scilab - 从 Scilab 中的方程计算离散序列