Xamarin选项卡导致System.NullReferenceException

时间:2015-02-26 10:38:57

标签: c# android xamarin xamarin.android

我正在尝试在我的Xamarin Android应用程序中实现简单的标签界面。 我的活动:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace SSMobile
{
    [Activity (Label = "Tabs", MainLauncher = false, Icon = "@drawable/icon",Theme = "@android:style/Theme.NoTitleBar")]
    public class MainActivity : Activity 
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
            SetContentView (Resource.Layout.Main);

            AddTab ("Panic", Resource.Drawable.tab_icon, new PanicFragment ());
            AddTab ("Diagnostics", Resource.Drawable.tab_icon, new DiagnosticsFragment ());
            AddTab ("Config", Resource.Drawable.tab_icon, new ConfigFragment ());
        }

        protected override void OnSaveInstanceState(Bundle outState)
        {
            outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);

            base.OnSaveInstanceState(outState);
        }

        void AddTab (string tabText, int iconResourceId, Fragment view)
        {
            var tab = this.ActionBar.NewTab ();            
            tab.SetText (tabText);
            tab.SetIcon (Resource.Drawable.tab_icon);

            // must set event handler before adding tab
            tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
            {
                var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
                if (fragment != null)
                    e.FragmentTransaction.Remove(fragment);         
                e.FragmentTransaction.Add (Resource.Id.fragmentContainer, view);
            };
            tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e) {
                e.FragmentTransaction.Remove(view);
            };

            this.ActionBar.AddTab (tab);
        }

        class PanicFragment: Fragment {
            public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                base.OnCreateView (inflater, container, savedInstanceState);
                var view = inflater.Inflate (Resource.Layout.PanicFragment, container, false);
                return view;
            }
        }

        class DiagnosticsFragment: Fragment {
            public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                base.OnCreateView (inflater, container, savedInstanceState);
                var view = inflater.Inflate (Resource.Layout.DiagnosticsFragment, container, false);
                return view;
            }
        }

        class ConfigFragment: Fragment {
            public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                base.OnCreateView (inflater, container, savedInstanceState);
                var view = inflater.Inflate (Resource.Layout.ConfigFragment, container, false);
                return view;
            }
        }
    }
}

当我的应用程序打开该活动时,我收到错误:

  

System.NullReferenceException:未将对象引用设置为实例   对象

在这一行:

this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

异常日志:

System.NullReferenceException: Object reference not set to an instance of an object
  at SSMobile.MainActivity.OnCreate (Android.OS.Bundle) [0x00009] in c:\Users\darek\Documents\Projects\SSMobile\SSMobile\MainActivity.cs:17
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1944
  at at (wrapper dynamic-method) object.3a5a286c-d028-4989-a49e-efabfb6b0ae6 (intptr,intptr,intptr) <IL 0x00017, 0x0001f>

此代码有什么问题?

1 个答案:

答案 0 :(得分:7)

我发现了我无法使用ActionBar的原因。 在我的活动中,我使用的是隐藏TitleBar的主题:

Theme = "@android:style/Theme.NoTitleBar"

这也使ActionBar隐藏起来并且不可用。 因此,如果您想拥有ActionBar,则无法使用该主题。如果您仍然需要隐藏标题栏,只需添加以下内容:

this.ActionBar.SetDisplayShowHomeEnabled (false);
this.ActionBar.SetDisplayShowTitleEnabled (false);

它会隐藏应用图标和标题。

希望它能为您节省几个小时的搜索时间。)