Search code examples
androidandroid-fragmentstabsandroid-tabhostfragment-tab-host

Android - Adding tabs to a Fragment


I am making a Fragment with tabs but I am having a problem that I cannot resolve. When adding the tabs I must set a content with setContent() but I just can set an Activity or a layout and I want to make a transaction between two fragments. I have this exception:

java.lang.IllegalArgumentException: you must specify a way to create the tab content
        at android.widget.TabHost.addTab(TabHost.java:225)
        at com.rpstudios.pokecooker.CustomFragment.onCreateView(CustomFragment.java:27)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)

Here is the code which is throwing the exception:

FragmentTabHost mTabHost;

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

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


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

    mTabHost.addTab(mTabHost.newTabSpec("TAB1").setIndicator("TAB1"));
    mTabHost.addTab(mTabHost.newTabSpec("TAB2").setIndicator("TAB2"));
    mTabHost.addTab(mTabHost.newTabSpec("TAB3").setIndicator("TAB3"));

    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String s) {
            if(s.equals("TAB1")){

            }else if(s.equals("TAB2")){

            }else if(s.equals("TAB2")){

            }
        }
    });


    return rootView;
}

And here is the tabhost_layout.xml :

<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>

Solution

  • According to Jesse's Answer:

    You should add the tab like this:

    mTabHost.addTab(mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon)), TheAssociatedFragmentTab.class, null);
    

    And you need an ExplorerFragment class which Overrides onCreateView and inflates the layout for your explorer tab:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
    {
      super.onCreateView(inflater, container, savedInstanceState);
      return inflater.inflate(R.layout.explorer_fragment, container,false);
    }