我正在尝试在我的Xamarin Android应用程序中实现简单的Tab界面。
我的活动:
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>
这段代码有什么问题?