我是Xamarin.Android的Android開發新手,我想了解如何解決下一個問題。Xamarin.Android中的默認構造函數
有時從後臺恢復我的Android應用程序後,我面臨的下一個錯誤: Unable to find the default constructor on type MainMenuFragment.
的MainMenuFragment
使用應用程序NavigationDrawerActivity允許用戶在應用程序中不同的片段之間切換。
爲了解決這個問題,我添加了一個默認的構造函數的MainMenuFragment
爲下一個環節裏面描述:
- 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.
但現在有時在將來,MainMenuFragment
將使用其默認構造進行初始化構造函數和它試圖訪問在第一時間其navigationDrawer它拋出一個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>
我怎麼能有沒有遇到這種空引用異常實現一個默認的構造?