Android FragmentTabHost : No tab known for tag nul

2019-01-17 14:02发布

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>

7条回答
We Are One
2楼-- · 2019-01-17 14:41

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);
        }
    }
}
查看更多
登录 后发表回答