gpt4 book ai didi

android - MaterialAlertDialogBu​​ilder 在自定义 View editText 上崩溃

转载 作者:行者123 更新时间:2023-12-04 04:31:27 25 4
gpt4 key购买 nike

尝试读取 editText 内容时,AlertDialog(Material) 崩溃。

警报对话框:

MaterialAlertDialogBuilder(activity)
.setTitle(title)
.setMessage(message)
.setView(R.layout.dialog_settings_entry)
.setPositiveButton(getString(R.string.text_change)) { dialog, which ->
etUserInput.hint = message
sgr = etUserInput.text.toString() // << crashes here
dialog.dismiss()
}
.setNegativeButton(getString(android.R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
.show()

单击肯定按钮后,结果如下:
    java.lang.IllegalStateException: etUserInput must not be null
at com.home.profile.SettingsFragment$buildAlertDialog$1.onClick(SettingsFragment.kt:332)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)

etUserInput 是单独布局中的简单editText。
不确定崩溃原因。将不胜感激任何洞察它或任何有用的 Material 样本。

最佳答案

DialogInterfaceAlertDialog然后使用 findViewById .

Kotlin :

val et = (dialog as? AlertDialog)?.findViewById<EditText>(R.id.etUserInput)
val text = et?.text.toString()

--

java :
EditText et = ((AlertDialog)dialog).findViewById(R.id.etUserInput);
String text = et.getText().toString();

--
MaterialAlertDialogBuilder(activity)
.setTitle(title)
.setMessage(message)
.setView(R.layout.dialog_settings_entry)
.setPositiveButton(getString(R.string.text_change)) { dialog, which ->
val text = (dialog as? AlertDialog)?.findViewById<EditText>(R.id.etUserInput)?.text?.toString()

dialog.dismiss()
}
.setNegativeButton(getString(android.R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
.show()

关于android - MaterialAlertDialogBu​​ilder 在自定义 View editText 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58957039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com