Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓tabHost?_Android_Android Tabhost - Fatal编程技术网

Android 安卓tabHost?

Android 安卓tabHost?,android,android-tabhost,Android,Android Tabhost,我试图创建一个选项卡主机,但我似乎无法使其工作,我做错了什么?我已经在onCreate方法上设置了createTab,但它崩溃了 我只是创建一个包含大约5个选项卡的tabhost。如果有人知道如何实现的话,我也在尝试实现滑动视图 主要活动 public class MainActivity extends Activity { TabHost tab; @Override protected void onCreate(Bundle savedInstanceState) { supe

我试图创建一个选项卡主机,但我似乎无法使其工作,我做错了什么?我已经在onCreate方法上设置了createTab,但它崩溃了

我只是创建一个包含大约5个选项卡的tabhost。如果有人知道如何实现的话,我也在尝试实现滑动视图

主要活动

public class MainActivity extends Activity {

TabHost tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

private void createTab() {
    TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");
    tab1Spec.setIndicator("TAB 1");
    Intent p = new Intent(this, tab1.class);
    tab1Spec.setContent(p);

    TabHost.TabSpec tab2Spec = tab.newTabSpec("tab2");
    tab2Spec.setIndicator("TAB 2");
    Intent p = new Intent(this, tab2.class);
    tab2Spec.setContent(p);

    tab.addTab(tab1Spec);
    tab.addTab(tab2Spec);
}
}

活动\u主xml

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

        <TabWidget
            android:id="@+id/tab"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"></TabWidget>

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

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

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

您没有初始化选项卡主机,然后在这一行中使用了它,这会引发NullPointerException:

TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");
在使用选项卡之前,请将其初始化为:

tab = (TabHost)this.findViewById(android.R.id.tabhost)

例外情况是什么?我已经设置好了,但是它说我仍然需要将tabhost id设置为android.r.id.tabhost