应用程序崩溃,因为延迟初始化 属性 尚未在 android studio 中初始化
app crashes as the late init property has not been initialized in android studio
我有一个按钮,当我点击它时,它应该显示警报对话框,但是当我点击那个按钮时应用程序崩溃了我知道代码流是这样的但我不知道我在哪里做错了
private lateinit var btn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dynam)
btn = findViewById(R.id.add1)
buildDialog()
btn.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
dialog.show()
}
})
}
private fun buildDialog() {
val builder=android.app.AlertDialog.Builder(this)
val view=layoutInflater.inflate(R.layout.dialog_dynam,null)
val name= view.findViewById<EditText>(R.id.nameEdit)
builder.setView(view)
builder.setTitle("Enter name")
.setPositiveButton("Ok",object :DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, p1: Int) {
addname(name.text.toString())
}`
我认为您将 dialog
作为 lateinit
变量,但您忘记在 buildDialog
函数中分配它。
private lateinit var btn: Button
private lateinit var dialog:AlertDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dynam)
btn = findViewById(R.id.add1)
buildDialog()
btn.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
if (this@YourActivity::dialog.isInitialized) { // to check if dialog is initialize or not
dialog.show()
}
}
})
}
private fun buildDialog() {
val builder=android.app.AlertDialog.Builder(this)
val view=layoutInflater.inflate(R.layout.dialog_dynam,null)
val name= view.findViewById<EditText>(R.id.nameEdit)
builder.setView(view)
builder.setTitle("Enter name")
.setPositiveButton("Ok",object :DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, p1: Int) {
addname(name.text.toString())
// initialize dialog
dialog = builder.create()
}
我有一个按钮,当我点击它时,它应该显示警报对话框,但是当我点击那个按钮时应用程序崩溃了我知道代码流是这样的但我不知道我在哪里做错了
private lateinit var btn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dynam)
btn = findViewById(R.id.add1)
buildDialog()
btn.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
dialog.show()
}
})
}
private fun buildDialog() {
val builder=android.app.AlertDialog.Builder(this)
val view=layoutInflater.inflate(R.layout.dialog_dynam,null)
val name= view.findViewById<EditText>(R.id.nameEdit)
builder.setView(view)
builder.setTitle("Enter name")
.setPositiveButton("Ok",object :DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, p1: Int) {
addname(name.text.toString())
}`
我认为您将 dialog
作为 lateinit
变量,但您忘记在 buildDialog
函数中分配它。
private lateinit var btn: Button
private lateinit var dialog:AlertDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dynam)
btn = findViewById(R.id.add1)
buildDialog()
btn.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
if (this@YourActivity::dialog.isInitialized) { // to check if dialog is initialize or not
dialog.show()
}
}
})
}
private fun buildDialog() {
val builder=android.app.AlertDialog.Builder(this)
val view=layoutInflater.inflate(R.layout.dialog_dynam,null)
val name= view.findViewById<EditText>(R.id.nameEdit)
builder.setView(view)
builder.setTitle("Enter name")
.setPositiveButton("Ok",object :DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, p1: Int) {
addname(name.text.toString())
// initialize dialog
dialog = builder.create()
}