36

I am trying to add a TabHost inside a Fragment. The code is given below. Here inside the Fragment. I am trying to add TabHost to show two Tabs:

package com.nordicsoft.dilosysNewVersion;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

public class Fragment_Coupons extends Fragment {
    TabHost tabHost;
    TabHost.TabSpec spec;

    public Fragment_Coupons() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_coupon, container,
                false);
        tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
        tabHost.setup();
        //Add_Text_To_Tab("Android", "IOS");

        Intent intentAndroid = new Intent().setClass(getActivity(),
                Aclass.class);
        spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
                .setIndicator("Android");

        tabHost.addTab(spec);

        Intent intentBus = new Intent().setClass(getActivity(), Bclass.class);
        spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
                .setContent(intentBus).setIndicator("Welcome");
        tabHost.addTab(spec);
        return rootView;
    }

}

The XML code:

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

This Is giving me the error:

12-09 17:40:53.509: E/AndroidRuntime(18103): FATAL EXCEPTION: main
12-09 17:40:53.509: E/AndroidRuntime(18103): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:690)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.widget.TabHost.setCurrentTab(TabHost.java:356)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.widget.TabHost.addTab(TabHost.java:246)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.nordicsoft.dilosysNewVersion.Fragment_Coupons.onCreateView(Fragment_Coupons.java:32)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.app.BackStackRecord.run(BackStackRecord.java:622)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.os.Handler.handleCallback(Handler.java:605)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.os.Looper.loop(Looper.java:137)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at android.app.ActivityThread.main(ActivityThread.java:4517)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at java.lang.reflect.Method.invoke(Method.java:511)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at dalvik.system.NativeStart.main(Native Method)
12-09 17:46:02.770: I/Process(18103): Sending signal. PID: 18103 SIG: 9
Behnam
  • 6,510
  • 6
  • 35
  • 65
NRahman
  • 2,717
  • 5
  • 24
  • 29
  • I have the exact same problem and simply cannot find a solution. Have you perhaps figured this out yet? – Chris Apr 09 '14 at 09:54

2 Answers2

68

Try to do this to handle the Tabs:

public class MainFragment extends Fragment {
    private FragmentTabHost mTabHost;

    //Mandatory Constructor
    public MainFragment() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);


        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                FragmentB.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
                FragmentC.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                FragmentD.class, null);


        return rootView;
    }
}

With the layout:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

The MotherActivity to host the MainFragment:

public class MotherActivity extends FragmentActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MainFragment fragmenttab = new MainFragment();
        getSupportFragmentManager().beginTransaction()
        .add(R.id.item_detail_container, fragmenttab).commit();


    }

And MotherActivity layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 />

After this just create the normal fragment B and C, etc Class. The Result will be:

Tab Fragment inside Fragment Result

ElCharro
  • 711
  • 4
  • 5
  • 4
    I'm having an error Exception raised during rendering: No tab known for tag null on the fragment's layout xml – AllenC Apr 09 '14 at 14:56
  • 1
    @ElCharro .... Thanks for this Solution .... this is very useful when combining drawer and tab effect ...without pager, slider etc ! ... [+1] – Devrath Apr 24 '14 at 20:08
  • @Nepster I'm unable to get rid of the `No tab known for tag null on the fragment's layout xml` error. Any help with that? – krtkush Sep 11 '14 at 20:40
  • you can create FragmentTabHost programatically in code instead of xml.. It may help otherwise this tutorial may help..http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/ – Zar E Ahmer Sep 12 '14 at 06:50
  • Please be informed about overlap of tabs problem which could appear here, see bug report https://code.google.com/p/android/issues/detail?id=53230 – Sergii Oct 26 '14 at 13:21
  • @Devrath how did you solved navigationdrawer and tabhost combining problem..I struck with it..can you help? – Aditya Vyas-Lakhan Jan 22 '15 at 13:38
  • Hello, I have followed this tutorial, but I am struggling to find the best way to colour/style the tabs. Can anybody help me with this? – Michael Murphy Feb 07 '16 at 08:44
  • This really worked for me! I just had to change android.support.v4.app.FragmentTabHost to android.support.v13.app.FragmentTabHost. – Vitor Braga Sep 16 '16 at 15:25
1

Try adding this line

tabHost.setup(this.getActivity().getLocalActivityManager());

before the line

tabHost.addTab(spec);

or try adding:

LocalActivityManager mLocalActivityManager = new LocalActivityManager(this.getActivity(), false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
host.setup(mLocalActivityManager);
SalGad
  • 2,991
  • 2
  • 18
  • 25