Android Alert Dialog cannot find view

I'm having trouble getting an AlertDialog to pass text back to its activity. It seems like the problem is that when findViewByID is called, the correct EditText cannot be found, but I'm new to Android and don't know why this is possible.

Code 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; @Override 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); builder.setView(modifyView); final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText); builder.setPositiveButton(R.string.modify, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { mListener.onMDialogPositiveClick(editText.getText().toString()); } }); 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 @Override public void onAttach(Activity activity) { super.onAttach(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 file:

 <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/modificationText" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="4dp"/> 

Why is the text editText not found? What can I do to make this work as intended by passing a new line into action?

+7
java android android-edittext android-dialogfragment
source share
4 answers

Edit

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

to

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

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

+9
source share

Your layout is bloating, and you have it builder.setView(modifyView);

So, to initialize edittext replace

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

by

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

findViewById searches for the view with the identifier specified in the current bloated layout. You do not need to getActivity to use the inflated view object instead to initialize your EditText.

 public final Activity getActivity () Added in API level 11 Return the Activity this fragment is currently associated with. 
+2
source share

What, because you are viewing a view from an action, try using editView.findView ...

+1
source share

If you are new to android, you should be aware of this.

In action, you simply findViewById any of your opinions that relates to the action layout. But in dialog , dialogFragments or custom views, you must manually reference the current layout, for example

view.findViewById (...)

Since your code referenced its parent activity, the application tried to find the identifier in action, but in fact your identifier belongs to your custom view, modifyView .

So, this is the right way for your code to find this identifier.

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

All Articles