Android Tab widget
<p>I am using below code to create three tab in activity but it gives me null pointer exception when I add tab using addtab method.</p>

<pre><code>            tabHost.addTab(firstTabSpec);//Null Pointer error occured here
            tabHost.addTab(secondTabSpec);
            tabHost.addTab(thirdTabSpec);
</code></pre>

<p>Below is my Code both layout xml file and onCreaate() method where I create tabs and put actvity in it.</p>

<pre><code>    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            try {
                /** TabHost will have Tabs */
                TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

                /**
                 * TabSpec used to create a new tab. By using TabSpec only we can
                 * able to setContent to the tab. By using TabSpec setIndicator() we
                 * can set name to tab.
                 */

                /** tid1 is firstTabSpec Id. Its used to access outside. */
                TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
                TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
                TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");

                /** TabSpec setIndicator() is used to set name for the tab. */
                /**
                 * TabSpec setContent() is used to set content for a particular tab.
                 */
                firstTabSpec.setIndicator("Update-1").setContent(
                        new Intent(this, Keyboard.class));
                secondTabSpec.setIndicator("Update-2").setContent(
                        new Intent(this, Sensors.class));
                thirdTabSpec.setIndicator("Update-3").setContent(
                        new Intent(this, Relays.class));

                /** Add tabSpec to the TabHost to display. */
                tabHost.addTab(firstTabSpec);//Null Pointer error occured here
                tabHost.addTab(secondTabSpec);
                tabHost.addTab(thirdTabSpec);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

Layout main.xml file code


&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" &gt;
&lt;TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" &gt;

    &lt;LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" &gt;

        &lt;TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" &gt;
        &lt;/TabWidget&gt;

        &lt;FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" &gt;
        &lt;/FrameLayout&gt;
    &lt;/LinearLayout&gt;

&lt;/TabHost&gt;
</code></pre>

<p>Edited XML file</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" &gt;



&lt;TabHost 
    android:id="@+id/tabhost1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" &gt;

    &lt;LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" &gt;

        &lt;TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" &gt;
        &lt;/TabWidget&gt;

        &lt;FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" &gt;
        &lt;/FrameLayout&gt;
    &lt;/LinearLayout&gt;

&lt;/TabHost&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>Logcat:</p>

<pre><code> java.lang.NullPointerException
    at android.widget.TabHost.addTab(TabHost.java:221)
    at com.android.embededconversation.EmbededConversation.onCreate(EmbededConversation.java:51)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
</code></pre>
