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 9285725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:14:22+00:00 2026-06-18T19:14:22+00:00

as we know tab Activity is deprecated now so some tutorials says me to

  • 0

as we know tab Activity is deprecated now so some tutorials says me to use FragmentActivity instead. and I am using this and its gives me null pointer exception on this line

 tabHost.addTab(spec);

My code is below.

//MainActivity.java

public class MainActivity extends FragmentActivity  {
public final static int HOME = 1;
public final static int CAMPUS = 2;
public final static int REPORTING = 3;
public final static int HELP = 4;
public final static int WISH = 5;
long transactionID = -1;
public static TabHost tabHost;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mytabs);
    MyView view = null;

    tabHost = (TabHost)findViewById(R.id.my_tabhost);
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, TabGroup1Activity.class);
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
    view.setFocusable(true);
    spec = tabHost.newTabSpec("Home").setIndicator(view).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup2Activity.class);
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
    view.setFocusable(true);
    spec = tabHost.newTabSpec("Campus").setIndicator(view)
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup3Activity.class);
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
    view.setFocusable(true);
    spec = tabHost.newTabSpec("Reporting").setIndicator(view)
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup4Activity.class);
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
    view.setFocusable(true);
    spec = tabHost.newTabSpec("Help").setIndicator(view)
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup5Activity.class);
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
    view.setFocusable(true);
    spec = tabHost.newTabSpec("Wish").setIndicator(view).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(4).getLayoutParams().height = LayoutParams.WRAP_CONTENT;

    tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(3).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
    tabHost.getTabWidget().getChildAt(4).getLayoutParams().width = LayoutParams.WRAP_CONTENT;

    if (MyApp.getFrom().equals("Home")) {
        tabHost.setCurrentTab(0);
    } else if (MyApp.getFrom().equals("Campus")) {
        tabHost.setCurrentTab(1);
    } else if (MyApp.getFrom().equals("Reporting")) {
        tabHost.setCurrentTab(2);
    } else if (MyApp.getFrom().equals("Help")) {
        tabHost.setCurrentTab(3);
    } else if (MyApp.getFrom().equals("Wish")) {
        tabHost.setCurrentTab(4);
    }

    int type = 0;
    if (getIntent().getExtras() != null) {
        if (getIntent().getExtras().containsKey("from")) {
            type = getIntent().getExtras().getInt("from");
            switch (type) {
            case HOME:
                tabHost.setCurrentTab(0);
            case CAMPUS:
                tabHost.setCurrentTab(1);
            case REPORTING:
                tabHost.setCurrentTab(2);
            case HELP:
                tabHost.setCurrentTab(3);
            case WISH:
                tabHost.setCurrentTab(4);
            default:
                tabHost.setCurrentTab(0);
            }
        }
    }
}

public long getTransactionID() {
    return transactionID;
}

public void setTransactionID(long l) {
    transactionID = l;
}

public void switchTabSpecial(int tab) {
    tabHost.setCurrentTab(tab);
}

class ChangeTabReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        tabHost = (TabHost)findViewById(R.id.my_tabhost);
        tabHost.setCurrentTab(1);
    }
}

private class MyView extends LinearLayout {
    ImageView iv;

    public MyView(Context c, int drawable, int drawableselec, String label) {
        super(c);

        iv = new ImageView(c);
        StateListDrawable listDrawable = new StateListDrawable();
        listDrawable.addState(SELECTED_STATE_SET, this.getResources()
                .getDrawable(drawable));
        listDrawable.addState(ENABLED_STATE_SET, this.getResources()
                .getDrawable(drawableselec));
        iv.setImageDrawable(listDrawable);
        iv.setBackgroundColor(Color.TRANSPARENT);
        iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, (float) 0.0));
        setGravity(Gravity.CENTER);
        addView(iv);
    }
}

}

//mytabs.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="@+id/my_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"
        android:padding="0dp" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:padding="0dp" >
        </FrameLayout>

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

// Error

02-05 10:57:22.301: E/AndroidRuntime(911): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccc.loyolacollege/com.ccc.loyolacollege.MainActivity}: java.lang.NullPointerException
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.os.Looper.loop(Looper.java:137)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-05 10:57:22.301: E/AndroidRuntime(911):  at java.lang.reflect.Method.invokeNative(Native Method)
02-05 10:57:22.301: E/AndroidRuntime(911):  at java.lang.reflect.Method.invoke(Method.java:511)
02-05 10:57:22.301: E/AndroidRuntime(911):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-05 10:57:22.301: E/AndroidRuntime(911):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-05 10:57:22.301: E/AndroidRuntime(911):  at dalvik.system.NativeStart.main(Native Method)
02-05 10:57:22.301: E/AndroidRuntime(911): Caused by: java.lang.NullPointerException
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.widget.TabHost.addTab(TabHost.java:229)
02-05 10:57:22.301: E/AndroidRuntime(911):  at com.ccc.loyolacollege.MainActivity.onCreate(MainActivity.java:38)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.Activity.performCreate(Activity.java:4465)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-05 10:57:22.301: E/AndroidRuntime(911):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  • 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-06-18T19:14:24+00:00Added an answer on June 18, 2026 at 7:14 pm

    You have not intialized the TabHost. Add the below line in your onCreate().

    tabHost = getTabHost();
    

    Also change the layout file TabHost id to android:id="@android:id/tabhost".

    Check out my below layout.

      <?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"
        android:padding="0dp" >
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:padding="0dp" >
        </FrameLayout>
       <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center|center" 
            android:tabStripEnabled="false"/>       
    </LinearLayout>
      </TabHost>
     </LinearLayout>
    

    And in your activity extends the TabActivity instead of FragmentActivity.Then run your application.

    I hope it will help you.

    Thanks.

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

Sidebar

Related Questions

i was planning to that way, i dont know how to use tab activity
I know how to develop the tab activity using xml layout and also i
I use a TabActivityGroup to open a second activity inside the same tab. Now
I am using Fragment activity in place of tab Group Activity. like this. public
I know you can do this to get vertical text in a tab header:
I created a Custom Tab Navigator and now I need to know how can
I am using a Tab based Activity in my App and I want to
I would like to know how to retrieve some new activity like the stackoverflow
I have created two tabs in my Activity using the below code. This is
(I know that Activity Groups and Tabs have been deprecated. I cannot redesign my

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.