Question

I have an activity in which i am supposed to use TabBar , I have already extended this activity from other class so i cant extend TabActivity.

I read one solution to use tabHost.setup() HERE.

But i am not able to execute that way.getting NullPointerException.

If someone can help me with proper example.

Here is the XML file

   <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


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

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

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

    </LinearLayout>

</TabHost>

Here is the Java File.

public class TabActivityActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
    tabHost.setup();
  }

}

Here is the Log Cat Error

10-20 15:21:05.196: ERROR/AndroidRuntime(6265): Uncaught handler: thread main exiting due to uncaught exception 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): java.lang.NullPointerException 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.view.ViewRoot.handleMessage(ViewRoot.java:1819) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.os.Handler.dispatchMessage(Handler.java:99) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.os.Looper.loop(Looper.java:123) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at android.app.ActivityThread.main(ActivityThread.java:4363) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at java.lang.reflect.Method.invokeNative(Native Method) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at java.lang.reflect.Method.invoke(Method.java:521) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 10-20 15:21:05.206: ERROR/AndroidRuntime(6265): at dalvik.system.NativeStart.main(Native Method)

On adding second tab its giving following error

10-20 16:46:23.361: ERROR/AndroidRuntime(1471): Uncaught handler: thread main exiting due to uncaught exception 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tab/com.tab.TabActivityActivity}: java.lang.RuntimeException: Could not create tab content because could not find view with id 2130903041 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.os.Handler.dispatchMessage(Handler.java:99) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.os.Looper.loop(Looper.java:123) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.ActivityThread.main(ActivityThread.java:4363) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at java.lang.reflect.Method.invokeNative(Native Method) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at java.lang.reflect.Method.invoke(Method.java:521) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at dalvik.system.NativeStart.main(Native Method) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2130903041 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.widget.TabHost$ViewIdContentStrategy.(TabHost.java:587) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.widget.TabHost$ViewIdContentStrategy.(TabHost.java:578) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.widget.TabHost$TabSpec.setContent(TabHost.java:435) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at com.tab.TabActivityActivity.onCreate(TabActivityActivity.java:22) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 10-20 16:46:23.381: ERROR/AndroidRuntime(1471): ... 11 more

Was it helpful?

Solution

Put at least one tab content into it

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
    tabHost.setup();

    TabSpec tab1 = tabHost.newTabSpec("TAB_1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(R.id.tab1);
    tabHost.addTab(tab1);

    //tab 2 etc...
    TabSpec tab2 = tabHost.newTabSpec("TAB_2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(R.id.tab2);
    tabHost.addTab(tab2);
}

and in the xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


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

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

        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_height="fill_parent" android:layout_width="fill_parent">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab1">
                <!-- tab 1 content goes here -->
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab2">
                <!-- tab 2 content goes here -->
            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

</TabHost>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top