lateinit 属性 绑定尚未初始化,但我没有将其设置为 lateinit
lateinit property binding has not been initialized though i did not set it as lateinit
我在尝试使用新的 ViewBinding 更新我的视图时遇到了这个错误。我没有将值定义为“lateinit”,但 logccat 说“lateinit 属性 绑定尚未初始化”,为什么我要使用它?
提前致谢。
私人 val 电子邮件和密码行除外。
class MainActivity : AppCompatActivity() {
private lateinit var auth : FirebaseAuth
private lateinit var binding: ActivityMainBinding
private val email = binding.emailText.text.toString()
private val password = binding.passwordText.text.toString()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
auth= FirebaseAuth.getInstance()
val guncelKullanici = auth.currentUser
if (guncelKullanici!= null) {
val intent = Intent(this, haber_akisi::class.java)
startActivity(intent)
finish()
}
}
fun girisYap ( view: View) {
if (email.isNotBlank() && password.isNotBlank()) {
auth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val intent = Intent(this,haber_akisi::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(this,exception.localizedMessage,Toast.LENGTH_LONG).show()
}}else {
Toast.makeText(this,"Lütfen E-mail ve Password alanlarını doldurunuz",Toast.LENGTH_LONG).show()
}
}
fun kayitOl ( view : View) {
if ( email.isNotBlank() && password.isNotBlank() ) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val intent = Intent(this, haber_akisi::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(this, exception.localizedMessage, Toast.LENGTH_LONG).show()
}
}else {
Toast.makeText(this,"Lütfen E-mail ve Password alanlarını doldurunuz",Toast.LENGTH_LONG).show()
}
}
}
这是因为 private val email = binding.emailText.text.toString()
在初始化之前使用了“绑定”变量。错误是说“lateinit var binding”尚未初始化,但您正在 private val email = binding.emailText.text.toString()
上访问它
编辑:解决这个问题的一种方法是将电子邮件和密码也设置为 lateinit 变量。另一种方法是没有电子邮件和密码 class 级别的属性,只在需要的地方访问绑定,就像在 girisYap() 和 kayitOl()
中一样
您将在其声明后的下一行中访问绑定(ActivityMainBinding 标识符)。
使用前需要初始化绑定
我在尝试使用新的 ViewBinding 更新我的视图时遇到了这个错误。我没有将值定义为“lateinit”,但 logccat 说“lateinit 属性 绑定尚未初始化”,为什么我要使用它?
提前致谢。
私人 val 电子邮件和密码行除外。
class MainActivity : AppCompatActivity() {
private lateinit var auth : FirebaseAuth
private lateinit var binding: ActivityMainBinding
private val email = binding.emailText.text.toString()
private val password = binding.passwordText.text.toString()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
auth= FirebaseAuth.getInstance()
val guncelKullanici = auth.currentUser
if (guncelKullanici!= null) {
val intent = Intent(this, haber_akisi::class.java)
startActivity(intent)
finish()
}
}
fun girisYap ( view: View) {
if (email.isNotBlank() && password.isNotBlank()) {
auth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val intent = Intent(this,haber_akisi::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(this,exception.localizedMessage,Toast.LENGTH_LONG).show()
}}else {
Toast.makeText(this,"Lütfen E-mail ve Password alanlarını doldurunuz",Toast.LENGTH_LONG).show()
}
}
fun kayitOl ( view : View) {
if ( email.isNotBlank() && password.isNotBlank() ) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val intent = Intent(this, haber_akisi::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(this, exception.localizedMessage, Toast.LENGTH_LONG).show()
}
}else {
Toast.makeText(this,"Lütfen E-mail ve Password alanlarını doldurunuz",Toast.LENGTH_LONG).show()
}
}
}
这是因为 private val email = binding.emailText.text.toString()
在初始化之前使用了“绑定”变量。错误是说“lateinit var binding”尚未初始化,但您正在 private val email = binding.emailText.text.toString()
编辑:解决这个问题的一种方法是将电子邮件和密码也设置为 lateinit 变量。另一种方法是没有电子邮件和密码 class 级别的属性,只在需要的地方访问绑定,就像在 girisYap() 和 kayitOl()
中一样您将在其声明后的下一行中访问绑定(ActivityMainBinding 标识符)。
使用前需要初始化绑定