なぜ機能しないのかわかりませんが、何が間違っているのでしょうか。TextView を明示的に設定し、LayoutInflater による Inflate メソッドを使用しないと、正常に動作します。実行時にアプリケーションを閉じるだけで、[テスト] タブに切り替えると、出力ウィンドウにエラーが表示されずにクラッシュします。
public class MyProfileActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
ActionBar actionBar = ActionBar;
actionBar.NavigationMode = ActionBarNavigationMode.Tabs;
actionBar.SetDisplayShowTitleEnabled (false);
ActionBar.Tab tab = actionBar.NewTab ();
tab.SetText ("Home");
// tab.SetIcon (Resource.Drawable.tab_home);
tab.SetTabListener(new TabListener<HomeFragment>(this, "home"));
actionBar.AddTab (tab);
tab = actionBar.NewTab ();
tab.SetText("Terms");
// tab.SetIcon(Resource.Drawable.tab_terms);
tab.SetTabListener (new TabListener<TermsFragment> (this, "terms"));
actionBar.AddTab (tab);
tab = actionBar.NewTab();
tab.SetText("test");
// tab.SetIcon(Resource.Drawable.tab_terms);
tab.SetTabListener(new TabListener<TestFragment>(this, "test"));
actionBar.AddTab(tab);
}
}
フラグメント
public class TestFragment : Fragment
{
private View _fragmentView;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.Inflate(Resource.Layout.Tabs, container);
return _fragmentView;
}
}
#region HomeFragment
public class HomeFragment : Fragment
{
public HomeFragment() : base() {}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
TextView tv = new TextView (Activity);
tv.Text = "Home";
return tv;
}
}
#endregion
#region TermsFragment
public class TermsFragment : Fragment
{
public TermsFragment() : base() {}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
TextView tv = new TextView (Activity);
tv.Text = "Terms";
return tv;
}
}
#endregion
タブリスナー
public class TabListener<T> : Java.Lang.Object, ActionBar.ITabListener where T : Fragment
{
#region Members
private Fragment m_fragment;
private readonly Activity m_activity;
private readonly string m_tag;
private readonly T m_fragmentType;
#endregion
#region Constructor
public TabListener(Activity activity, String tag)
{
// The Activity is our host Activity and provides the Context.
// The type T and the tag will be used to instantiate the fragment.
// m_fragment will hold the instance for reuse after it is first created.
m_activity = activity;
m_tag = tag;
}
#endregion
#region ITabListener implementation
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
// Instantiate the reference and add it to the container,
// or attach it if it has already been created
if (m_fragment == null)
{
m_fragment = Fragment.Instantiate(m_activity, Java.Lang.Class.FromType(typeof(T)).Name);
ft.Add(global::Android.Resource.Id.Content, m_fragment, m_tag);
}
else
{
ft.Show(m_fragment);
}
}
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
if (m_fragment != null)
{
ft.Hide(m_fragment);
}
}
public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// User selected the already selected tab. Usually do nothing.
}
#endregion
public class TestFragment : Fragment
{
private View _fragmentView;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.Inflate(Resource.Layout.Tabs, container);
return _fragmentView;
}
}
}