title: How to take text input with DialogFragment in Android?
body: <p>I am trying to get a value that user enters into a Dialog, using the recommended <code>DialogFragment</code> class for it, the Dialog constructs and runs fine, but I cannot return the value of the <code>EditText</code> parameter to the parent class, without get a <code>NullPointerException</code>.</p>
  <p>My <code>DialogHost</code> class, this constructs, returns and links the parent to its buttons.</p>
  <pre><code>package jo.app.co;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.DialogFragment;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.view.LayoutInflater;
   
  public class DialogHost extends DialogFragment {
  
  
  public interface NoticeDialogListener {
      public void onDialogPositiveClick(DialogFragment dialog);
      public void onDialogNegativeClick(DialogFragment dialog);
  }
  
  NoticeDialogListener mListener;
  
  @override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      
      try {
          
          mListener = (NoticeDialogListener) activity;
          
      } catch (ClassCastException e) {      
          
          throw new ClassCastException(activity.toString());
      }
  }
  
  @override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      
      LayoutInflater inflater = getActivity().getLayoutInflater();
  
      builder.setView(inflater.inflate(R.layout.dialog_add, null))
     
             .setPositiveButton(&quot;Save&quot;, new DialogInterface.OnClickListener() {
                 
                 @override
                 public void onClick(DialogInterface dialog, int id) {        
                     mListener.onDialogPositiveClick(DialogHost.this);                       
                     
                 }
             })
             .setNegativeButton(&quot;Cancel&quot;, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     DialogHost.this.getDialog().cancel();                       
                 }
             });      
      return builder.create();
  }
  
  }
  </code></pre>
  <p>This is my <code>MainActivity</code> class showing the dialog</p>
  <pre><code>package jo.app.co;
  
  import android.app.DialogFragment;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.util.Log;
  import android.widget.EditText;
  
  public class MainActivity extends FragmentActivity implements      DialogHost.NoticeDialogListener {
  
  
   @override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      showNoticeDialog();
      
  }
  
  public void showNoticeDialog() {
      
      DialogFragment dialog = new DialogHost();
      dialog.show(getFragmentManager(), &quot;DialogHost&quot;);
  }
  
  
  @override
  public void onDialogPositiveClick(DialogFragment dialog) {
              
      EditText myText = (EditText) findViewById(R.id.item_added);
      try {
          Log.d (&quot;IN TRY&quot;, myText.getText().toString());          
      }
      catch (Exception e) {
          Log.e (&quot;IN CATCH&quot;, e.toString());
      }           
      
  }
  
  @override
  public void onDialogNegativeClick(DialogFragment dialog) {        
      Log.d (&quot;INMAIN&quot;, &quot;REACHED NEG&quot;);
  }
  
  
   }
  </code></pre>
  <p>Thisthe layout of my Dialog for reference.</p>
  <pre><code>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
     android:orientation=&quot;vertical&quot;
     android:layout_width=&quot;wrap_content&quot;
     android:layout_height=&quot;wrap_content&quot;&gt;
     
      &lt;EditText
         android:id=&quot;@+id/item_added&quot;
         android:inputType=&quot;text&quot;
         android:layout_width=&quot;match_parent&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_marginTop=&quot;16dp&quot;
         android:layout_marginLeft=&quot;4dp&quot;
         android:layout_marginRight=&quot;4dp&quot;
         android:layout_marginBottom=&quot;4dp&quot;
         android:hint=&quot;@string/hint_add_item&quot; /&gt;
     
  &lt;/LinearLayout&gt;
  </code></pre>