我有一个导航抽屉应用程序,我正在使用Fragments
。我正在尝试创建一个使用Fragment
标签的FragmentTabHost
。我遇到的问题是我在选项卡中预期的内容不存在。这只是一个空白标签。
Fragment
为Android.Support.V4.App.Fragment
。
这是我的代码:
class MyTabFragment : Fragment
{
private FragmentTabHost mTabHost;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mTabHost = new FragmentTabHost(Activity);
mTabHost.Setup(Activity, ChildFragmentManager);
mTabHost.AddTab(mTabHost.NewTabSpec("ATab").SetIndicator("ATab"),
Java.Lang.Class.FromType(typeof(MyCustomFragment)), null);
return mTabHost;
}
public override void OnDestroyView()
{
base.OnDestroyView();
mTabHost = null;
}
}
或者我试过这个:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.fragment_tabhost, container, false);
}
public override void OnStart()
{
base.OnStart();
mTabHost = View.FindViewById<FragmentTabHost>(Resource.Id.tabhost);
mTabHost.Setup(Activity, ChildFragmentManager, Resource.Id.realtabcontent);
mTabHost.AddTab(mTabHost.NewTabSpec("ATab").SetIndicator("ATab"),
Java.Lang.Class.FromType(typeof(MyCustomFragment)), null);
}
的xml:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
但在那种情况下,我会抛出一个致命的异常。这是堆栈跟踪:
FATAL EXCEPTION: main
java.lang.IllegalStateException: No tab known for tag null
at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:330)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280)
at android.view.View.dispatchAttachedToWindow(View.java:11937)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2415)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
at android.view.ViewGroup.addView(ViewGroup.java:3210)
at android.view.ViewGroup.addView(ViewGroup.java:3155)
at android.view.ViewGroup.addView(ViewGroup.java:3131)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我明白了。
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.fragment_tabhost, container, false);
mTabHost = rootView.FindViewById<FragmentTabHost>(Resource.Id.tabhost);
mTabHost.Setup(Activity, ChildFragmentManager, Resource.Id.realtabcontent);
mTabHost.AddTab(mTabHost.NewTabSpec("ATab").SetIndicator("ATab"),
Java.Lang.Class.FromType(typeof(MyCustomFragment)), null);
return rootView;
}