TabHost defined in layout provides a NullPointerException during setContent

I am trying to change Tabs1.javafrom Android API Demos 16 (and run on API 16 emulator) to use the standard Activity, not the deprecated one TabActivity. I created a new layout file and changed the code.

The reason I'm trying to use this approach, unlike the action bar, is because the tabs do not apply to the entire screen, but only a smaller one for viewing.

When I execute the code, I get NullPointerExceptioninside the Android method setContentwhen the following line is executed:

tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.view1)

Any ideas why this might happen? Full stack trace after code and layout.

Tabs1VanillaActivity.java:

public class Tabs1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs1);
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);

        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.view1));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab2")
                .setContent(R.id.view2));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab3")
                .setContent(R.id.view3));
    }
}

tabs1.xml:     

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView goes here!"
        tools:ignore="HardcodedText" />

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

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

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

            <FrameLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/view1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/blue"
                    android:text="@string/tabs_1_tab_1" />

                <TextView
                    android:id="@+id/view2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/red"
                    android:text="@string/tabs_1_tab_2" />

                <TextView
                    android:id="@+id/view3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/green"
                    android:text="@string/tabs_1_tab_3" />
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

Stack trace:

09-18 16:18:22.968: E/AndroidRuntime(1191): FATAL EXCEPTION: main
09-18 16:18:22.968: E/AndroidRuntime(1191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.apis/com.example.android.apis.view.Tabs1}: java.lang.NullPointerException
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.os.Looper.loop(Looper.java:137)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at java.lang.reflect.Method.invoke(Method.java:511)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at dalvik.system.NativeStart.main(Native Method)
09-18 16:18:22.968: E/AndroidRuntime(1191): Caused by: java.lang.NullPointerException
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:617)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:612)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.widget.TabHost$TabSpec.setContent(TabHost.java:461)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at com.example.android.apis.view.Tabs1.onCreate(Tabs1.java:42)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.Activity.performCreate(Activity.java:4465)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-18 16:18:22.968: E/AndroidRuntime(1191):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-18 16:18:22.968: E/AndroidRuntime(1191):     ... 11 more
+5
2

TabHost documentation, , -, tabHost.setup(), TabActivity:

setup() TabHost findViewById(). . setup() getTabHost() TabActivity. :

mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
+3

All Articles