Home » Android » java – Android Alert Dialog unable to find view

java – Android Alert Dialog unable to find view

Posted by: admin June 15, 2020 Leave a comment


I’m having trouble getting an AlertDialog to pass text back to the activity that calls it. It seems the issue is that it fails to find the proper EditText when calling findViewByID, but I’m new to Android and don’t know why that may be.

The code is below:

public class ModifyDialogFragment extends DialogFragment {

/* The activity that creates an instance of this dialog fragment must
 * implement this interface in order to receive event callbacks.
 * Each method passes the DialogFragment in case the host needs to query it. */
public interface MDialogListener {
    public void onMDialogPositiveClick(String newValue);

// Use this instance of the interface to deliver action events
MDialogListener mListener;

String mEntryName = "";
EditText mEditText;

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    final View modifyView = inflater.inflate(R.layout.modify_dialog, null);


    final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText);

           builder.setPositiveButton(R.string.modify, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
           builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog

    // Create the AlertDialog object and return it
    return builder.create();

// Override the Fragment.onAttach() method to instantiate the ModifyDeleteDialogListener
public void onAttach(Activity activity) {
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the MDDialogListener so we can send events to the host
        mListener = (MDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement MDialogListener");

And the corresponding modify_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>

<EditText xmlns:android="http://schemas.android.com/apk/res/android"

Why isn’t the editText being found? What can I do to make this work as intended, passing the new string back to the activity?

How to&Answers:


final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText);


final EditText editText = (EditText) modifyView.findViewById(R.id.modificationText);

Your EditText lives in modify_dialog.xml so you need to use the variable that was inflated with that layout (here modifyView) to find the id not the layout that getActivty() will look in.


Your inflating a layout and you have this builder.setView(modifyView);

So to initialize edittext replace

 final EditText editText = (EditText)getActivity().findViewById(R.id.modificationText);


 final EditText editText = (EditText) modifyViewfindViewById(R.id.modificationText);

findViewById looks for a view with id provided in the current inflated layout. You don’t need getActivity instead use theinflated view object to initialize your EditText.

public final Activity getActivity ()

Added in API level 11
Return the Activity this fragment is currently associated with.


That’s because you are searching the view from the activity try using modifyView.findView…


Just this

EditText editText = (EditText) modifyView.findViewById(R.id.modificationText);

What was the problem?

In activity you just findViewById any view you want which refers to activity layout.
But in dialog, dialogFragments, or custom views you have to refer the current layout manually, like


As your code was referring to its parent activity so app tried to find the id in activity but actually your id belongs to your custom view modifyView.

So this is correct way for your code to find that id.