I have a button that when clicked should call a method. The program will build but I am getting a runtime error System.NullReferenceException when the method is being called. How is the method null? The exception occurs at the comment line.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
_countdown = (TextView)FindViewById(Resource.Id.countdown);
Button buttonStop = FindViewById<Button>(Resource.Id.stopButton);
//exception happens below
buttonStop.Click += stopButton_Click;
Button buttonSet = FindViewById<Button>(Resource.Id.setButton);
buttonSet.Click += setButton_Click;
Button buttonClear = FindViewById<Button>(Resource.Id.clearButton);
buttonClear.Click += clearButton_Click;
var back = FindViewById<Button>(Resource.Id.buttonBack);
back.Click += (sender, e) =>
{
StartActivity(typeof(DM_App_Code_Fragment.MainScreen));
};
...
}
This is the method to be called.
private void stopButton_Click(object sender, EventArgs e)
{
StopTimer();
}
private void StopTimer()
{
_timer.Enabled = false;
}
This is the code related to _timer
//initialization
private System.Timers.Timer _timer = new System.Timers.Timer();
The below code occurs in OnCreate method.
_timer.Interval = 1000;
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_timer.Enabled = false;
Also this is a group effort with two Resource.Designer.cs files. There are two of these in the files: One in Resource.Designer.cs and the other in Resource.DesignerQT.cs so I commented out one of them because I was getting an error Duplicate "global::Android.Runtime.ResourceDesignerAttribute" attribute. Now I think this is why the button is null because the Resource files are not loading properly. Is there a way to have two Resource.Designer.cs files in a project or is only one allowed?
[assembly:global::Android.Runtime.ResourceDesignerAttribute("QuickTimer.Resource",
IsApplication=true)]
This is the xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Quick Timer"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewQuickTimer" />
<TextView
android:text="Hour"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_below="@id/textViewQuickTimer"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/textViewHours" />
<TextView
android:text="Min"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignBaseline="@id/textViewHours"
android:layout_alignBottom="@id/textViewHours"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewMinutes" />
<TextView
android:text="Sec"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignBaseline="@id/textViewMinutes"
android:layout_alignBottom="@id/textViewMinutes"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:id="@+id/textViewSeconds" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewHours"
android:prompt="@string/hours"
android:layout_marginLeft="34dp"
android:id="@+id/spinnerHours" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewMinutes"
android:layout_marginLeft="130dp"
android:prompt="@string/minutes"
android:id="@+id/spinnerMinutes" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewSeconds"
android:layout_marginLeft="220dp"
android:prompt="@string/seconds"
android:id="@+id/spinnerSeconds" />
<Button
Click="setButton_Click"
android:text="Set Timer / Start"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinnerMinutes"
android:id="@+id/setButton"
android:layout_marginLeft="95dp"
android:clickable="true" />
<TextView
android:text="Timer Countdown"
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/setButton"
android:id="@+id/countdown" />
<Button
Click="stopButton_Click"
android:text="Stop"
android:id="@+id/stopButton"
android:layout_below="@id/countdown"
android:layout_marginLeft="65dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
Click="clearButton_Click"
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="205dp"
android:layout_below="@id/countdown"
android:id="@+id/clearButton" />
<Button
android:text="Back"
android:layout_marginTop="35dp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/clearButton"
android:id="@+id/buttonBack" />
</RelativeLayout>