Reputation: 17
I'm currently getting a null reference exception in Xamarin.Android on the call to sendChildInfoToMainForm() below. I have no problem manipulating the properties of either object prior to passing it, and when the exception is thrown the inspector doesn't show either object as null. Any ideas what might be happening here? I'm baffled although I feel like it must be something simple.
public override Android.Views.View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.popupAddChild, container, true);
Dialog.SetTitle ("New Child");
EditText childFirstName_editText = view.FindViewById<EditText> (Resource.Id.editTextChildFirstName);
DatePicker childDOB_datePicker = view.FindViewById<DatePicker> (Resource.Id.datePickerChidDOB);
Button submitChild = view.FindViewById<Button> (Resource.Id.buttonAddChild);
childDOB_datePicker.SpinnersShown = true;
//set up min and max dates
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = System.DateTime.UtcNow.AddYears(-26) - origin;
childDOB_datePicker.MinDate = (long) Math.Floor(diff.TotalSeconds);
//wire up the button
submitChild.Click += delegate { sendChildInfoToMainForm(childFirstName_editText, childDOB_datePicker); };
return view;
}
private void sendChildInfoToMainForm(EditText kidName, DatePicker kidDOB)
{
((AddMemberActivity) this.Activity).currentNewMember.childName1 = kidName.Text;
((AddMemberActivity) this.Activity).currentNewMember.childDOB1 = kidDOB.DateTime.ToShortDateString();
Dismiss ();
}
Upvotes: 0
Views: 246
Reputation: 24460
Put in a breakpoint and debug your way out of it. My suspicion is that it is actually not the kidName
nor kidDOB
that are null, but rather your crazy casting and your assumption that you can set your data in the Activity
like you are doing.
A better way of achieving what you are doing is to make the DialogFragment
not knowing anything about the Activity
showing it. This also makes it a lot easier to reuse it in other places in the Activity
if you need to do that at some point.
So instead of having the method sendChildInfoToMainForm
. I would instead create an event, passing the actual data, such that the Activity
can handle what to do with it, rather than giving the DialogFragment
this responsibility.
public class ChildDialogEventArgs : EventArgs {
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
public delegate void ChildDialogEventHandler(object sender, ChildDialogEventArgs e);
Then in your DialogFragment
:
public event ChildDialogEventHandler Changed;
and then in your Click
event do this instead:
submitChild.Click += (_, __) => {
var handler = Changed;
if (handler != null)
handler(this, new ChildDialogEventArgs {
Name = childFirstName_editText.Text,
DateOfBirth = childDOB_datePicker.DateTime
});
}
Then in your Activity
you simply need to subscribe to the Changed
event of your DialogFragment
and handle what to do with the data there.
Upvotes: 1