我收到错误:对象引用未设置为对象的实例

时间:2014-08-13 09:28:28

标签: android xamarin

我在尝试设置导航模式时如何解决它

[Activity(Label = "@string/app_name", MainLauncher = true)]

public class MainActivity : Activity,ActionBar.ITabListener {

     private ActionBar ab;
     protected override void OnCreate (Bundle bundle)
      {

        base.OnCreate (bundle);
        try{
           //here i am getting instance of action  bar
            ab = ActionBar;
        **i am getting error at this line **

                     ab.NavigationMode = ActionBarNavigationMode.Tabs;


            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);



}
}

1 个答案:

答案 0 :(得分:2)

我运行了你的代码,唯一的例外是我没有暗示ITabListener,而且它有效。

[Activity (Label = "@string/app_name", MainLauncher = true)]

    public class MainActivity : Activity
    {

        private ActionBar ab;

        protected override void OnCreate (Bundle bundle)
        {

            base.OnCreate (bundle);


try {
            //here i am getting instance of action  bar
            ab = ActionBar;
        **i am getting error at this line **

            ab.NavigationMode = ActionBarNavigationMode.Tabs;


            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);



        } catch (Exception ex) {
            throw ex;
        }
    }

}

我从来没有见过有人使用动作栏来暗示ITabListener(在xamarin上)。我会采取xamarin的方式来建立一个动作栏。 Here是文档的链接。