在安卓中与标签片段



我有2个片段FragmentA和FragmentB。FragmentA 位于 FragmentB 之上。现在我想在 FragmentB 上添加选项卡。请帮忙。到目前为止我的代码:-

布局/片段A.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top Fragment" />
</LinearLayout>

布局/片段B.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F6CE"
    android:orientation="vertical" >
    <TabHost
        android:id="@android: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="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <include 
                    android:id="@+id/tabdata1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    layout=“@layout/tabdata1_view"/>
                <include 
                    android:id="@+id/tabdata2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    layout=“@layout/tabdata2_view"
                    />
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

碎片B.java

public class Fragment2 extends Fragment{
    TabHost myTabHost;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment2_layout, container, false);
        myTabHost = (TabHost) getView().findViewById(R.id.tabhost)
        return rootView;
    }
}

错误:-

行" myTabHost = (TabHost) getView().findViewById(R.id.tabhost)"给出错误,因为它找不到选项卡主机。

请提出一种方法来实现此目的。

试试这个

myTabHost = (TabHost) rootview.findViewById(android.R.id.tabhost);

这应该可以消除您现在面临的错误

这是因为在你从onCreateView返回之前,片段实际上还没有视图。你只需要相对于rootView调用findViewById。喜欢这个:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView =  inflater.inflate(R.layout.fragment2_layout, container, false);
    myTabHost = (TabHost) rootView.findViewById(R.id.tabhost)
    return rootView;
}

最新更新