【问题标题】:TabHost not working on a FragmentTabHost 无法处理片段
【发布时间】:2017-03-27 08:12:40
【问题描述】:

我已经设法在我的fragment 上添加了TabHost,但是当我点击一个标签时,我希望另一个fragment 进入FrameLayout,另一个也是如此。

我已尝试应用该方法,但在 setup host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent) 中不起作用。

有什么办法可以解决这个问题?

这是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >


    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="30dp" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                </LinearLayout>

            </FrameLayout>
        </LinearLayout>


    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLinear"
        />
    </TabHost>




</RelativeLayout>

这是我的 Java 代码:

public class HomeFragment extends Fragment {

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);


    TabHost host = (TabHost) view.findViewById(R.id.tabHost);
    host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);


    host.addTab(host.newTabSpec("tab11").setIndicator("Tab11"), PhotosFragment1.class, null);


    host.addTab(host.newTabSpec("tab22").setIndicator("Tab22"), PhotosFragment2.class, null);


    return view;


 }


}

【问题讨论】:

  • 您能否尝试将您的TabHost 替换为FragmentTabHost 并致电host.setup(getActivity(), getFragmentManager(), R.id.realtabcontent);
  • 我第一次使用 FragmentTabHost 但我在布局和 java 中不断收到空指针异常...所以切换到 TabHost
  • 正如question 的答案中所述,您应该能够毫无例外地实现它。我也运行良好。
  • 渲染过程中引发异常:没有已知标签为 null 的选项卡这是 xml 中发生的情况
  • 好吧 tnx 兄弟,当我完美运行它时它对我有用....但是当我看到它仍然显示的 xml 文件时(渲染期间引发的异常:没有标签已知标签为空)

标签: android android-fragments android-tabhost


【解决方案1】:

将您的 HomeFragments onCreateView() 更改为

View view = inflater.inflate( R.layout.fragment_home, container, false );

FragmentTabHost host = ( FragmentTabHost ) view.findViewById( R.id.tabHost );
host.setup(getActivity(), getFragmentManager(), R.id.realtabcontent);

host.addTab( host.newTabSpec( "tab11" ).setIndicator( "Tab11" ), PhotosFragment1.class, null );
host.addTab( host.newTabSpec( "tab22" ).setIndicator( "Tab22" ), PhotosFragment2.class, null );

return view;

在你的fragment_home.xml 中寻找

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<android.support.v4.app.FragmentTabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="30dp"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLinear"/>

</android.support.v4.app.FragmentTabHost>

让你的PhotosFragment1PhotosFragment2 喜欢

public class PhotosFragment1 extends Fragment
{
    @Nullable
    @Override
    public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) 
    {
        return View.inflate( getContext(), R.layout.fragment_photos_1, null ); 
    } 
}

它应该可以正常工作。

【讨论】:

  • 好吧 tnx 兄弟,当我完美运行它时它对我有用....但是当我看到它仍然显示的 xml 文件时(渲染期间引发的异常:没有标签已知标签为空)
  • @RajivReddy 看看这个question。可能有关于您的问题的其他信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多