I am new to Android development with Xamarin.Android and I would like to understand how to have the next issue fixed.
Sometimes after restoring my Android application from background I was facing the next error:
Unable to find the default constructor on type MainMenuFragment.
The MainMenuFragment
is used by the application NavigationDrawerActivity to allow users to switch between different Fragments inside the app.
In order to solve it, I added a default constructor to the MainMenuFragment
as described inside the next links:
- Xamarin Limitations - 2.1. Missing constructors
Added a default constructor, should fix the issue.
public class MainMenuFragment : DialogFragment { readonly NavigationDrawerActivity navigationDrawer; #region Constructors public MainMenuFragment () {} // Default constructor... public MainMenuFragment (NavigationDrawerActivity navigationDrawer, IMenuType launchMenu = null) { if (navigationDrawer == null) throw new ArgumentNullException ("navigationDrawer"); this.navigationDrawer = navigationDrawer; ... Fragment UpdateTopFragmentForCurrentMenu (Fragment newMenuRootFragment = null) { Fragment currentMenuRootFragment = navigationDrawer.CurrentFragment; // issued line.
But now sometime in the future, the MainMenuFragment
gets initialized using its default constructor and at the first time it tries to access its navigationDrawer it throws a System.NullReferenceException
:
System.NullReferenceException: Object reference not set to an instance of an object
at MainMenuFragment.UpdateTopFragmentForCurrentMenu (Android.App.Fragment) <0x00018>
at MainMenuFragment.OpenMenu (IMenuType,bool) <0x0006b>
at MainMenuFragment.OnCreate (Android.OS.Bundle) <0x00053>
at Android.App.Fragment.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005b>
at (wrapper dynamic-method) object.3919a6ec-60c1-49fd-b101-86191363dc45 (intptr,intptr,intptr) <0x00043>
How can I have a default constructor implemented without facing this null reference exception?