Search code examples
javaandroidandroid-edittextdialog

How to find Edittext by id in a dialog?


I have a Dialog with an EditText, there, it asks for a wallet, and users write it.

After that, this wallet should be stored in a String, but it says that it's null.

Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

If I use an EditText from the Activity, it works fine. But it fails when I do it from the Dialog EditText (eTW).

There is my code.

private void showDialog(){
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.layout_walletdialog);
    dialog.getWindow().setBackgroundDrawableResource(R.drawable.bg_window);

    ImageView btnClose = dialog.findViewById(R.id.btn_close);
    Button btnSend = dialog.findViewById(R.id.btn_yes);
    EditText wallet = (EditText) findViewById(R.id.eTW);
    String value;

Solution

  • If eTW is defined in your walletdialog xml layout file, replace

    EditText wallet = findViewById(R.id.eTW);
    

    with

    EditText wallet = dialog.findViewById(R.id.eTW);