Android FragmentTabHost : No tab known for tag nul

2019-01-17 13:53发布

问题:

I used below code and it is not render graphical layout. display error as Exception raised during rendering: No tab known for tag null.

how can i solve this ?

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

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

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

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

回答1:

This is the code that I've used to initialise the TabHost and it works fine:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DetailFragment extends Fragment {

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // R.layout.fragment_tabs_pager contains the layout as specified in your question
        View rootView = inflater.inflate(R.layout.fragment_tabs_pager, container, false);

        // Initialise the tab-host
        FragmentTabHost mTabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        // Initialise this list somewhere with the content that should be displayed
        List<String> itemsToBeDisplayed;

        for (String subItem : itemsToBeDisplayed) {
            // Give along the name - you can use this to hand over an ID for example
            Bundle b = new Bundle();
            b.putString("TAB_ITEM_NAME", subItem);

            // Add a tab to the tabHost
            mTabHost.addTab(mTabHost.newTabSpec(subItem).setIndicator(subItem), YourContentFragment.class, b);
        }
        return rootView;
    }
}

/**
  * This class contains the actual content of a single tab  
  */
public class YourContentFragment extends Fragment {

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

        Bundle extras = getArguments();
        if (extras != null) {
            if (extras.containsKey("TAB_ITEM_NAME")) {
                String subItem = extras.getString("TAB_ITEM_NAME");
                // Do something with that string
            }
        }
    }
}


回答2:

It seems there is a bug in android support library.

I finally found this workaround:

Changed layout file as this one:

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

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" />

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

</LinearLayout>

Then i created by code FragmentTabHost as this example:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    tabHost = new FragmentTabHost(getActivity());
    inflater.inflate(R.layout.logs_view, tabHost);
    tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

    tabHost.addTab(tabHost.newTabSpec("simple").setIndicator("Simple"), ActivityLogFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec("contacts").setIndicator("Contacts"), MiniStatementFragment.class, null);
    return tabHost;
}


回答3:

I met this problem, researched, but could not found any working solution. But I notice that when I add a tab right after 'FragmentTabHost#setup', this problem does not occur, but for example, when I add a tab in 'AsyncTask#onPostExecute', this problem occurs. This seems stupid, but I tried, it is.. Based on those, I found a solution:

Adding an empty tab right after 'FragmentTabHost#setup', and then adding other tabs anywhere, I tried this solution, it works! I will the layout and partial codes below:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

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

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal"/>

</LinearLayout>

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    TabHost.TabSpec tabSpec = mTabHost.newTabSpec("TAB_FIRST").setIndicator("First");

    mTabHost.addTab(tabSpec, FirstFragment.class, null);

    TabWidget tabWidget = mTabHost.getTabWidget();
    if (tabWidget != null) {
        View tabWidgetChild = tabWidget.getChildAt(0);
        if (tabWidgetChild != null) {
            tabWidgetChild.setVisibility(View.GONE);
        }
    }
}


回答4:

I got a similar problem on using tabHost in fragments, searched a lot and finally found a solution to that.

1) Keep you code in on create view

2) This is the key -> While inflating the layout for tab indicators use as follows

View tabIndicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);

We need to set the parent value in the inflater as mTabHost.getTabWidget()

Setting tab indicator using the above code solved the issue of

          Java : illegal state exception : no tab know for tag null

Update :

mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);

mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

View tabIndicatorToday = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
        ((TextView) tabIndicatorToday.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.text));

mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.text)).setIndicator(tabIndicatorToday), Fragment.class, null);


回答5:

This problem occurs when you try to setup your FragmentTabHost in onViewCreated, which is too late. Try to setup it in onCreateView, and add at least one tab before returning the view object.



回答6:

My develop environment is Android Studio 0.8.9 with API19 SDK.

If I put the FragmentTabHost in a FragmentActivity, it works. When I put the FragmentTabHost in a Fragment, it gets "no tab known for tag null" when render and get runtime error when LayoutInflater inflate the layout.

Thank for user3216049's answer, it's a good workaround. Sorry, I cannot vote since I am a newbie. :(

However it display nothing in my test tab fragments. I did a small modification.

  • test_fragment.xml

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

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

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

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:orientation="horizontal" />

</LinearLayout>

  • fragment_section_dummy.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"
android:padding="32dp" />

  • Java code The point is that I change the id to the "R.id.realtabcontent" in FragmentTabHost.setup()

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;

public class TestFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, 
                ViewGroup container, Bundle savedInstanceState) {

        FragmentTabHost tabHost = new FragmentTabHost(getActivity());
        inflater.inflate(R.layout.test_fragment, tabHost);
        tabHost.setup(getActivity(), 
                      getChildFragmentManager(), R.id.realtabcontent);

        tabHost.addTab(tabHost.newTabSpec("simple")
            .setIndicator("Simple"), DummySectionFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("contacts")
            .setIndicator("Contacts"), DummySectionFragment.class, null);
        return tabHost;
    }

    /**
     * A dummy fragment representing a section of the app, 
     * but that simply displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        private static int count = 0;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_section_dummy, 
                   container, false);
            ((TextView) rootView.findViewById(android.R.id.text1))
                    .setText("Dummy Section " + count++);
            return rootView;
        }
    }
}



回答7:

i solve my problem by refering this link

FragmentTabHost - No tab known for tag null

No tab known for tag null its Means tabhost is not initialized yet becouse you are trrying in onCreateView method which is too late

call tabhost in onResume method