Android TabHost不处理片段
我已经成功地在我的Android TabHost不处理片段,android,android-fragments,android-tabhost,Android,Android Fragments,Android Tabhost,我已经成功地在我的片段上添加了TabHost,但是当我点击一个选项卡时,我希望另一个片段进入FrameLayout,这同样会进入另一个选项卡 我已尝试应用该方法,但在setuphost.setup(getActivity()、getChildFragmentManager()、R.id.realtabcontent)中它不起作用 如何解决这个问题 以下是我的XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/
片段上添加了TabHost
,但是当我点击一个选项卡时,我希望另一个片段进入FrameLayout
,这同样会进入另一个选项卡
我已尝试应用该方法,但在setuphost.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>
将您的HomeFragment
sonCreateView()
更改为
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\u 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>
它应该可以正常工作。您是否可以尝试用FragmentTabHost
替换TabHost
并调用host.setup(getActivity(),getFragmentManager(),R.id.realtabcontent)
我第一次使用FragmentTabHost,但我在layout和java中不断遇到空指针异常……因此切换到TabHost,正如回答中所述,您应该能够毫无例外地实现它。我也很好地运行了它。在呈现期间引发的异常:没有标签为null这是在xmlOkay tnx bro中发生的情况,当我完美地运行它时,它对我很有用……但是当我看到它仍然显示的xml文件时,仍然会出现异常(呈现期间引发的异常:没有标签为null)好的,tnx bro当我运行它时,它对我来说非常有效……但是当我看到它仍然显示的xml文件时(在呈现过程中引发的异常:没有标记为null的选项卡)@rajivredy看看这个。可能还有关于您的问题的其他信息
<?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>
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 );
}
}