android 自定义视图editText时MaterialAlertDialogBuilder崩溃

68de4m5k  于 2023-05-27  发布在  Android
关注(0)|答案(2)|浏览(170)

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

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。不确定崩溃原因。将感谢任何深入了解它或任何有用的材料样本。

3duebb1j

3duebb1j1#

DialogInterface转换为AlertDialog,然后使用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()
qlckcl4x

qlckcl4x2#

为我工作

LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_show_shopin,null);
        RecyclerView rvCarrito = dialogView.findViewById(R.id.rvCarrito);
        rvCarrito.setHasFixedSize(true);
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT);
        rvCarrito.setLayoutParams(lp);
        rvCarrito.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        CarritoAdapter adapterCarrito = new CarritoAdapter(getActivity(),listProductDetalle);
        rvCarrito.setAdapter(adapterCarrito);
        new MaterialAlertDialogBuilder(getActivity())
                .setCancelable(false)
                .setView(dialogView)
                .setPositiveButton("Ordenar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("Cerrar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .show();

相关问题