Search code examples
javaandroidfindviewbyid

findViewById respond "none"


I'm trying to getText() from EditText view in an AlertDialog. In the main activity in the method onCreate() I set the button listener.

public class MainActivity2 extends AppCompatActivity {

    private ActivityMain2Binding mainActivity2;
    private ArrayAdapter adapter;
    private ArrayList<simpleArrayAdapterElement> data;
    private final static String KEY_TITLE = "title";
    private final static String KEY_DESCRIPTION = "description";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity2 = ActivityMain2Binding.inflate(getLayoutInflater());
        View view = mainActivity2.getRoot();
        setContentView(view);
        arrayAdapter();
        mainActivity2.buttonIddItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog();
            }
        });
//        setupListViewSimple();
    }

Then I call the showCustomDialog() in onClick.

private void showCustomDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(R.layout.custom_dialog);
        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText alertDialogText = (EditText) findViewById(R.id.setText1);
                System.out.println(alertDialogText.getText());
                System.out.println(alertDialogText.getText().toString()); // just in case
            }
        });
        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

EditText alertDialogText = (EditText) findViewById(R.id.setText1); // return none

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/setText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        android:layout_marginTop="16dp"
        android:hint="Type your name here:"
        android:text="Some"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

*I also use binding in my activity.

What I'm doing wrong?


Solution

  • You are referring to MainActivity2 itself. you need to refer to its view by using LayoutInflater. You can check example answer here: https://stackoverflow.com/a/36793463/9346054

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.custom_dialog, null);
    
        builder.setView(dialogView);
        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText alertDialogText = dialogView.findViewById(R.id.setText1);
                System.out.println(alertDialogText.getText());
                System.out.println(alertDialogText.getText().toString()); // just in case
            }
        });
        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
            }
        });