0

I am trying to create a tab host to but I cant seem to make it work, what am i doing wrong? I have setup up createTab() on my onCreate method but it crashes.

I am simply creating a tabhost that holds around 5 tabs. Also I am trying to implement swipe view if anyone knows how to implement it

Main activity

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);
}

}

Activity_main 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>

DaveDave
  • 293
  • 1
  • 2
  • 10

1 Answers1

0

You didn't initialize your tab host , then you've used it in this line which throw a NullPointerException :

TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");

before using tab initialize it like :

tab = (TabHost)this.findViewById(android.R.id.tabhost)
Arash GM
  • 10,316
  • 6
  • 58
  • 76