Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default constructors in Xamarin.Android

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?

like image 584
georgepiva Avatar asked Mar 23 '23 02:03

georgepiva


1 Answers

You're programming like a C# developer, thats what the problem is :) I faced these same hurdles learning monodroid.

Take a look at the examples out there, in java, you'll see almost all the time they initialize using a static method like object.NewInstance() which returns object. This is how they initialize their views/receivers/fragments. At that point they populate the Arguments property and store that in the fragment. You need to remove all your constructors EXCEPT the empty ones and use arguments to pass your data around. If you try to do this using constructors and regular oo concepts you'll be in for a world of hurt. Arguments.putExtra and all those methods are there. It makes things a little verbose but once you get the hang of it you'll start creating some helper methods etc.

Once you get that sorted, you'll need to figure out if you need to recreate your fragments everytime the activity is resumed and if not, mark them as RetainInstance = true as well as get them onto a fragmentmanager which will help you retain all your state.

If you haven't built on android before it's weird and certainly not what I expected. But it's reeaaallly cool, much more awesome than I expected too. And same with Xamarin.

Great similar question: Best practice for instantiating a new Android Fragment

like image 70
TombMedia Avatar answered Apr 09 '23 08:04

TombMedia