gpt4 book ai didi

android - Xamarin Android - 在 fragment 中使用 FragmentTabHost

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:29 26 4
gpt4 key购买 nike

我正在制作一个抽屉导航应用程序,它使用 Fragments。我正在尝试创建一个 Fragment,它使用带有 FragmentTabHost 的选项卡。我遇到的问题是我在选项卡中期望的内容不存在。这只是一个空白标签。

FragmentAndroid.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)

最佳答案

我想通了。

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;
}

关于android - Xamarin Android - 在 fragment 中使用 FragmentTabHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26517218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com