This question is different from the marked duplicate as it does not go into detail about this specific example of delegates.
I would like to neatly add a delegate method to my button on click events. This is done using C# in Xamarin. When running the app in my emulator I am getting the below unhandled exception.
System.NullReferenceException: Object reference not set to an instance of an object.
Below is the code I am using
using System;
using Android.App;
using Android.Widget;
using Android.OS;
namespace AudioTour
{
[Activity(Label = "AudioTour", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Find Buttons
Button startButton = FindViewById<Button>(Resource.Id.startAudio);
Button stopButton = FindViewById<Button>(Resource.Id.stopAudio);
// Set Screen
SetContentView (Resource.Layout.Main);
// Assign Delegate Methods to Buttons
startButton.Click += StartAudio;
stopButton.Click += StopAudio;
}
// Start Audio Method
void StartAudio(object sender, EventArgs ea)
{
// Do Code
Console.WriteLine("Starting Audio");
}
// Stop Audio Method
void StopAudio(object sender, EventArgs ea)
{
// Do Code
Console.WriteLine("Stopping Audio");
}
}
}
Thanks