Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:12:14+00:00 2026-05-28T13:12:14+00:00

I am using below code to create three tab in activity but it gives

  • 0

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.

            tabHost.addTab(firstTabSpec);//Null Pointer error occured here
            tabHost.addTab(secondTabSpec);
            tabHost.addTab(thirdTabSpec);

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

    @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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <TabWidget
            android:id="@android:id/tabs"
            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" >
        </FrameLayout>
    </LinearLayout>

</TabHost>

Edited XML file

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



<TabHost 
    android:id="@+id/tabhost1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <TabWidget
            android:id="@android:id/tabs"
            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" >
        </FrameLayout>
    </LinearLayout>

</TabHost>

</LinearLayout>

Logcat:

 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:12:15+00:00Added an answer on May 28, 2026 at 1:12 pm

    This is wrong

     TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    

    you are trying to inflate from the wrong resource and the tabHost stays null, always.

    change it to :

     TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
    

    And make sure you include the correct R (your project’s R, not android.R)

    EDIT

    If you’re using TabActivity, the way to fetch the TabHost is, I added an example from an application I wrote:

    your TabActivity:

    public class yourTabActivity extends TabActivity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_tab_widget);
    
            // fetch the TabHost
            final TabHost tabHost = (TabHost) getTabHost();
    
            // create Tabs
            tabHost.addTab(createTab(yourActivity.class, 
                    "tag", "yourTabTitle", R.drawable.tv));
        }
    
        private TabSpec createTab(final Class<?> intentClass, final String tag, final String title, final int drawable)
        {
            final Intent intent = new Intent().setClass(this, intentClass);
            final View tab = LayoutInflater.from(getTabHost().getContext()).inflate(R.layout.tab, null);
            ((TextView) tab.findViewById(R.id.tab_text)).setText(title);
            ((ImageView) tab.findViewById(R.id.tab_icon)).setImageResource(drawable);
    
            return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent);
        }
    
    }
    

    main_tab_widget.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <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" >
    
                <HorizontalScrollView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="none" >
    
                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" />
                </HorizontalScrollView>
    
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
            </LinearLayout>
        </TabHost>
    
    </LinearLayout>
    

    example for tab.xml. this is actually a custom tab.

    <?xml version="1.0" encoding="utf-8"?>
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:background="@drawable/tab_bg_selector" 
            android:gravity="center"
            android:orientation="vertical" 
            android:padding="5dp">
    
            <ImageView android:id="@+id/tab_icon" 
                 android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:scaleType="fitCenter" />
    
            <TextView android:id="@+id/tab_text" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:singleLine="true"
                android:textStyle="bold" 
                android:gravity="center_horizontal"
                android:textSize="10sp" 
                android:ellipsize="marquee"
                android:textColor="@android:color/black" />
        </LinearLayout>
    

    This should cover everything you need for a tab widget…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to create a reminder in Google calendar (using Google
I'm trying to create a custom drop down and using the code below it
I am using jquery to create a custom dropdown with the code below. I
Okay, so I've got the following code shown below to create a dialog using
Hi all I am using below code to create an event : EKEvent *oneLabEvent
I'm getting some very jerky scrolling while using the code below to create a
i am using below code to change the the font type of text view.
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
In past, I am using Listview and using below code can show a particular
I want to enable auto update script in flex3. i am using below code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.