MaterialAlertDialogBuilder crashes on custom view editText
Asked Answered
N

2

9

The AlertDialog(Material) crashes when tries to read the editText content.

The 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()

On clicking the positive button results as follows:

    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)

The etUserInput is simple editText in a seperate layout. Unsure the crash reason. Would appreciate any insight into it or any helpful Material samples.

Numidia answered 20/11, 2019 at 14:45 Comment(4)
Where you initialize the EditText etUserInput? I think you forget to initialize itTribute
I am guessing that's kotlin syntetics? If so, is the view which contains the etUserInput inflated in the current screen?Strontian
Yes, etUserInput inflated via Kotlin synthetic import.Numidia
Even let approach tried, etUserInput.text?.let{ sgr = it.toString() } but no luck!Numidia
B
7

Cast the DialogInterface to an AlertDialog and then use 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()
Bromberg answered 13/3, 2020 at 3:28 Comment(0)
O
0

Work for me

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();
Orestes answered 25/5, 2023 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.