Fai*_*sal 7 android android-edittext illegalstateexception material-dialog
尝试读取 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()
Run Code Online (Sandbox Code Playgroud)
单击肯定按钮结果如下:
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)
Run Code Online (Sandbox Code Playgroud)
该etUserInput是在一个单独的布局简单EDITTEXT。不确定崩溃原因。希望对它有任何了解或任何有用的材料样本。
将 投射DialogInterface到 anAlertDialog然后使用findViewById。
科特林:
val et = (dialog as? AlertDialog)?.findViewById<EditText>(R.id.etUserInput)
val text = et?.text.toString()
Run Code Online (Sandbox Code Playgroud)
——
爪哇:
EditText et = ((AlertDialog)dialog).findViewById(R.id.etUserInput);
String text = et.getText().toString();
Run Code Online (Sandbox Code Playgroud)
——
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2470 次 |
| 最近记录: |