Xamarin.Android原生项目:将TabHost添加到项目中

时间:2018-03-13 14:26:12

标签: c# android xamarin xamarin.android

我正在使用Xamarin处理Android项目,我在我的视图中添加了一个TabHost,在OnCreateView中我调用了一个方法(SetupTabHost)来设置我的TabHost:

public void SetupTabHost(View view)
    {
        var mTabHost = view.FindViewById<FragmentTabHost>(Resource.Id.mTabHost);
        mTabHost.Setup(CurrentBaseActivity, CurrentBaseActivity.SupportFragmentManager, Resource.Id.realtabcontent);
        mTabHost.AddTab(mTabHost.NewTabSpec("Tab 1").SetIndicator("Tab 1"), Tab1Fragment.NewInstance().Class, null);
        mTabHost.AddTab(mTabHost.NewTabSpec("Tab 2").SetIndicator("Tab 1"), Tab2Fragment.NewInstance().Class, null);
        mTabHost.AddTab(mTabHost.NewTabSpec("Tab 3").SetIndicator("Tab 1"), Tab3Fragment.NewInstance().Class, null);

    }

我的OnCreateView:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.MyLayout, container, false);
        toolbar = this.Activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.Toolbar);
        SetupTabHost(view);
        bindings.Add(this.SetBinding(() => ViewModel.Title, () => toolbar.Title, BindingMode.OneWay));
        return view;
    }

但我得到了这个例外:(

未处理的例外情况: Java.Lang.IllegalStateException:FragmentManager已在执行事务

你能帮我吗?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

尝试将SetupTabHost()方法更改为以下内容,并告诉我它是否有效。

view.Post(() =>
{
    var mTabHost = view.FindViewById<FragmentTabHost>(Resource.Id.mTabHost);
    mTabHost.Setup(CurrentBaseActivity, CurrentBaseActivity.SupportFragmentManager, Resource.Id.realtabcontent);
    mTabHost.AddTab(mTabHost.NewTabSpec("Tab 1").SetIndicator("Tab 1"), Tab1Fragment.NewInstance().Class, null);
    mTabHost.AddTab(mTabHost.NewTabSpec("Tab 2").SetIndicator("Tab 1"), Tab2Fragment.NewInstance().Class, null);
    mTabHost.AddTab(mTabHost.NewTabSpec("Tab 3").SetIndicator("Tab 1"), Tab3Fragment.NewInstance().Class, null);
});