mercredi 4 février 2015

Xamarin Android - custom alert dialog with checkboxes, exception thrown

I have an Alert Dialog that pops up and gives the user several choices via checkboxes, as well as an edit text for the user to enter some text. The custom alert dialog inflates from a layout and all is good so far.


My problem is trying to handle the click events of the checkboxes. I keep getting null reference exceptions and i can't work out where i'm going wrong. I've tried writing the code in various fashions and i think i'm just blind to the problem now.


I need to see if the checkboxes are checked and assign the results to my bools.


The exception gets thrown at the point i try to handle my click event. Any help appreciated, thanks.



var myCustomAlert = LayoutInflater.Inflate(Resource.Layout.delegateCaptureAlertLayout, null);

bool wantphoneCall = true;
bool wantBrochure = false;
bool wantMailingList = false;
string additionalText;

AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
builder.SetTitle("Your request");
builder.SetView(myCustomAlert);
builder.SetMessage("Some message");
builder.SetPositiveButton("OK", delegate { // do something
});
builder.Show ();


CheckBox checkCallMe = FindViewById<CheckBox> (Resource.Id.checkBoxCallMe);
CheckBox checkBrochure = FindViewById<CheckBox> (Resource.Id.checkBoxBrochure);
CheckBox checkMailingList = FindViewById<CheckBox> (Resource.Id.checkBoxMailing);
EditText additionalInfoText = FindViewById<EditText> (Resource.Id.textAdditionalInfo);

checkCallMe.Click += (sender, e) => { // exception thrown here
Console.WriteLine("Checkbox is clicked");

if (checkCallMe.Checked)
{
wantphoneCall = true;
Console.WriteLine("Checkbox is checked");
}
else
{
wantphoneCall = false;
Console.WriteLine("Checkbox is not checked");
}




Aucun commentaire:

Enregistrer un commentaire