片段内的TabHost

时间:2012-09-05 14:36:04

标签: android android-fragments

我正在尝试在片段中添加TabHost布局:

public class FriendsTabFregment extends Fragment {

    private TabHost tab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_friends, container, false);
        tab = (TabHost) view.findViewById(R.activity_friends.tab);
        return view;
    }
}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);             
        addNewTab("Friends", BasicFragment.class, savedInstanceState);  //Chat Tab
        setTabHeight(50);       
    }

由于某种原因我将选项卡设为null。 这是我的XML:

<TabHost android:id="@+activity_friends/tab"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="4">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>


</LinearLayout>

任何想法为什么?

1 个答案:

答案 0 :(得分:0)

TabHost的ID定义为

android:id="@+id/activity_friends_tab"

发现它是这样的:

tab = (TabHost) view.findViewById(R.id.activity_friends_tab);

请查看此帖子,以进一步了解"@+id/"

What does "@+id" mean?

Android documentation source