我想创建Fragment Tab Bar.but它会出错?

时间:2013-12-12 07:20:11

标签: java android android-fragments android-tabhost android-tabs

我的代码在这里,但它很难显示在仿真器屏幕上你的应用程序停止了。

主。 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="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

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

主要Activity.Java

public class MainActivity extends FragmentActivity {

private FragmentTabHost mTabHost;
Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(context, getSupportFragmentManager(), R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1"),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2"),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3"),
            FragmentTab.class, null);
}

}

FragmentTab.Java

public class FragmentTab extends Fragment {

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(this.getTag() + " Content");
    return v;
}
}

fragment_layout.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/hello_world"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

但是当我运行此代码时,它会给出RunTime错误。我对此完全感到困惑,任何身体都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为你的id在xml中是错误的。您的TabHost必须是@android:id/tabhost(android.R.id.tabhost),而您的TabWidget必须是@android:id/tabs(android.R.id.tabs)

请参阅此简单示例Creating a Tab Layout with FragmentTabHost and Fragments

希望这有帮助。