title: Button Clicked function error in Xamarin
body: <p>I have a simple dialog fragment with a cancel and save button. When cancel is clicked dialog is dismissed. And when I clicked save button an error occur saying. "object reference not set to an instance of an object". I don't really understand where I got it wrong. </p>
  
  <p>Here is my axml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:local="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:paddingLeft="8dp"
      android:paddingRight="8dp"
      android:paddingTop="10dp"
      android:paddingBottom="10dp"&gt;
      &lt;TextView
          android:text="Set the number of days:"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/setnumDays" /&gt;
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/numberOfDays"
          android:layout_below="@+id/setnumDays"
          android:textSize="20sp"
          android:layout_marginTop="5dp"
          android:layout_centerHorizontal="true" /&gt;
      &lt;Button
          android:id="@+id/cancel"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:layout_below="@id/numberOfDays"
          android:text="Cancel"
          android:background="?android:attr/selectableItemBackground" /&gt;
      &lt;Button
          android:id="@+id/save"
          android:background="?android:attr/selectableItemBackground"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="5dp"
          android:layout_marginTop="10dp"
          android:layout_below="@id/numberOfDays"
          android:layout_toRightOf="@+id/cancel"
          android:layout_alignBaseline="@+id/cancel"
          android:text="Save" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My dialog fragment is:</p>
  
  <pre><code>using System;
  using Android.App;
  using Android.Content;
  using Android.Support.V4.App;
  using Android.Views;
  using Android.Widget;
  using MedAlert.Activities;
  using Fragment = Android.Support.V4.App.Fragment;
  //using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
  using Android.Support.V7.App;
  using Android.Support.Design.Widget;
  using V7Toolbar = Android.Support.V7.Widget.Toolbar;
  using Android.OS;
  
  namespace MedAlert.Fragments
  {
      public class DurationDialog : Android.Support.V4.App.DialogFragment
      {
          EditText numOfdays;
  
          public DurationDialog()
          {
              this.RetainInstance = true;
          }
  
          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
          {
              var ignored = base.OnCreateView(inflater, container, savedInstanceState);
              var view = inflater.Inflate(Resource.Layout.dialog_duration, container, false);
  
              var cancel = view.FindViewById&lt;Button&gt; (Resource.Id.cancel);
              var save = view.FindViewById&lt;Button&gt; (Resource.Id.save);
              numOfdays = view.FindViewById&lt;EditText&gt; (Resource.Id.numberOfDays);
  
              cancel.Click += (object sender, EventArgs e) =&gt; {
                  Dialog.Dismiss();
              };
  
              save.Click += Save_Click;
  
              return view;
          }
  
          void Save_Click (object sender, EventArgs e)
          {
              if (addMedFragment.mode == "ADD") {
                  try{
                      addMedFragment.duration = Int32.Parse(numOfDays.Text);
                      Dialog.Dismiss ();
                  }catch(Exception ex){
                      Toast.MakeText(Activity, ex.Message, ToastLength.Long).Show();
                      Dialog.Dismiss ();
                  }
              }
              if (addMedFragment.mode == "EDIT"){
                  try{
                      EDmedFragment.duration = Int32.Parse(numOfDays.Text);
                      Dialog.Dismiss ();
                  }catch(Exception ex){
                      Toast.MakeText(Activity, ex.Message, ToastLength.Long).Show();
                      Dialog.Dismiss ();
                  }
              }
          }
  
          public override void OnActivityCreated(Bundle savedInstanceState)
          {
              Dialog.Window.RequestFeature (WindowFeatures.NoTitle);
              base.OnActivityCreated (savedInstanceState);
          }
      }
  }
  </code></pre>
  
  <p>Please help. I really need to solve this one before the week ends. Thanks!
  Having these on my application output tab:</p>
  
  <pre><code>[MonoDroid] UNHANDLED EXCEPTION:
  [MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object
  [MonoDroid] at MedAlert.Fragments.DurationDialog.OnCreateView (Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle) [0x0003e] in c:\Users\xhemMiaow~\Documents\Projects\MedAlert\MedAlert\Fragments\DurationDialog.cs:34
  [MonoDroid] at Android.Support.V4.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (intptr,intptr,intptr,intptr,intptr) &lt;IL 0x00026, 0x000fa&gt;
  [MonoDroid] at (wrapper dynamic-method) object.9e951dbb-09ac-4bc9-accc-8ff86fa9323b (intptr,intptr,intptr,intptr,intptr) &lt;IL 0x00023, 0x00047&gt;
  </code></pre>