TabHost defined in layout is giving NullPointerException during setContent
Asked Answered
C

2

5

I'm trying to modify Tabs1.java from the Android API Demos 16 (and run on an API 16 emulator) to use a standard Activity rather than the deprecated TabActivity class. I've created a new layout file and modified the code.

The reason I'm trying to use this approach as opposed to the action bar is that the tabs don't apply to the entire screen, only a smaller sub-view.

When I execute the code, I'm getting a NullPointerException inside the Android setContent method when executing the following line:

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

Any ideas why this might be happening? Full stack trace is 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
Cookstove answered 18/9, 2012 at 16:19 Comment(0)
C
9

You must call tabHost.setup() before attempting to add tabs.

http://developer.android.com/reference/android/widget/TabHost.html#setup()

Condescendence answered 18/9, 2012 at 16:49 Comment(0)
C
3

I found in the TabHost documentation that apparently you need to call tabHost.setup() if you're not using a TabActivity:

Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity. Example:

mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
Cookstove answered 18/9, 2012 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.