Xamarin Android FrameLayout更改视图

时间:2018-03-26 14:58:29

标签: c# xamarin.android layout-inflater android-framelayout fragmentmanager

我正在尝试执行以下代码:

FrameLayout fragmentContainer = FindViewById<FrameLayout>(Resource.Id.fragmentContainer);
fragmentContainer.RemoveAllViews();
fragmentMain.view = fragmentMain.LayoutInflater.Inflate(Resource.Layout.MainLayoutPortrait, null, false);
fragmentContainer.AddView(fragmentMain.view);

但是当我到达我的代码的3d行并尝试获取fragmentMain.LayoutInflater:

fragmentMain.view = fragmentMain.LayoutInflater.Inflate(Resource.Layout.MainLayoutPortrait, null, false);

程序失败并出现错误:

onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.

1 个答案:

答案 0 :(得分:0)

尝试这样做:

    LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
        view = inflater.Inflate(Resource.Layout.Buttons, layout);

如果你在片段使用活动上下文中获取GetSystemService

古德勒克!