1

I am trying to get the user input from an AlertDialog within a Fragment. The layout worked all right but when I try input a value and retrieve it on a Toast, the app crashes. Below is my last try to build the AlertDialog.

       val editText : EditText? = view?.findViewById(R.id.outlinedTextField)

        fun showDialog(){
            val builder = AlertDialog.Builder(context)
            val inflater = layoutInflater
            val dialogLayout = inflater.inflate(R.layout.price_dialog, null)
            val text = editText.text.toString()

            with(builder) {
                setTitle("How much?")
                setPositiveButton("Confirm"){ _,_ ->
                    if(editText!!.text.isEmpty()){
                        Toast.makeText(context, "empty", Toast.LENGTH_LONG).show()
                    } else {
                        Toast.makeText(context, "$text", Toast.LENGTH_LONG).show()
                    }
                }
                setNegativeButton("Cancel"){ _,_ ->
                    Toast.makeText(context, "cancel", Toast.LENGTH_LONG).show()
                }
                setView(dialogLayout)
                show()
            }
        }```

0 Answers0