in the example of working with DialogFragment

( https://developer.android.com/guide/topics/ui/dialogs.html?hl=ru )

there is an implementation of the onDialogPositiveClick method

MainActivity:

 public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{ ... public void showNoticeDialog() { // Create an instance of the dialog fragment and show it DialogFragment dialog = new NoticeDialogFragment(); dialog.show(getSupportFragmentManager(), "NoticeDialogFragment"); } // The dialog fragment receives a reference to this Activity through the // Fragment.onAttach() callback, which it uses to call the following methods // defined by the NoticeDialogFragment.NoticeDialogListener interface @Override public void onDialogPositiveClick(DialogFragment dialog) { // User touched the dialog's positive button ... } @Override public void onDialogNegativeClick(DialogFragment dialog) { // User touched the dialog's negative button ... } } 

NoticeDialogFragment:

 public class NoticeDialogFragment 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 NoticeDialogListener { public void onDialogPositiveClick(DialogFragment dialog); public void onDialogNegativeClick(DialogFragment dialog); } // Use this instance of the interface to deliver action events NoticeDialogListener mListener; // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener @Override public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (NoticeDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } ... @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Build the dialog and set up the button click handlers AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.dialog, null)) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Send the positive button event back to the host activity mListener.onDialogPositiveClick(NoticeDialogFragment.this); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Send the negative button event back to the host activity mListener.onDialogNegativeClick(NoticeDialogFragment.this); } }); return builder.create(); } } 

EditText placed on the DialogFragment, it is required to get the entered data

tried so

 @Override public void onDialogPositiveClick(DialogFragment dialog) { EditText et = (EditText) dialog.getView().findViewById(R.id.editText); Log.d("myLog", et.getText().toString(); } 

but getting an error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById (int)' on a null object reference

question: how to get data from DialogFragment?

  • Something is not visible where you ask your markup for the dialogue. Maybe there is just no EditText? - Yuriy SPb
  • @YuriySPb, corrected the code, was originally the source from the example. There is an EditText, AndroidStudio sees it by ID, the feeling that at the time of the call the item is already deleted. When closing the dialog, the elements are not automatically deleted? - ravend

1 answer 1

By calling getView on a DialogFragment instance, you get the fragment markup, instead of the AlertDialog markup, which is in the fragment. Where applicable, refer to the internal markup as follows:

 dialog.getDialog().findViewById(...); 

Alternatively, transfer the values ​​instead of the dialog itself via the interface, having received the values ​​in the dialog itself, and not from the argument in the activation interface:

 LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.dialog, null); builder.setView(dialogView) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { EditText valueView = (EditText) dialogView.findViewById(R.id.editText); //here if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) 
  • one
    thanks, I had similar suspicions - ravend
  • I tried to pass the dialogView through the interface argument, it turned out to MainActivity to DialogFragment from the DialogFragment . Apparently the problem is not onDeattach , but the fact that the form loaded via the inflater cannot be accessed via getView - ravend
  • one
    @ravend, you may be right. Try this: dialog.getDialog().findViewById() or even like this: dialog.findViewById() - JuriySPb 2:44 pm
  • thanks, it worked dialog.getDialog().findViewById() - ravend
  • @ravend, supplemented the answer - YuriySPb