如何在片段中调用 AlertDialog?

how to call an AlertDialog inside a fragment?

我在一个片段中,我想使用 alertDialog 但我不明白。

senha_esquecida.setOnClickListener {
        val builder = AlertDialog.Builder(activity)
        builder.setTitle("Senha Esquecida:")
        val view = layoutInflater.inflate(R.layout.dialog_senha_esquecida, null)
        val nomeUsuario = view.findViewById<EditText>(R.id.et_nomeUsuario)
        builder.setView(view)
        builder.setPositiveButton("Reset", DialogInterface.OnClickListener { dialog, which ->
            forgotPassword(nomeUsuario)
        })
        builder.setNegativeButton("Close", DialogInterface.OnClickListener { dialog, which ->  })
        builder.show()
    }

我试图将 activity 作为参数传递,但它也不起作用,有人知道如何帮助我吗?

试试这个。

而不是调用 activity 调用 getActivity()

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Whosebug Question");
        builder.setMessage("This is the answer of Whosebug's question");
        builder.show();