43

我使用了下面的代码,它不是渲染图形布局。显示错误为Exception raised during rendering: No tab known for tag null

我该如何解决这个问题?

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

7 回答 7

8

这是我用来初始化 TabHost 的代码,它工作正常:

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
            }
        }
    }
}
于 2013-07-11T12:03:42.750 回答
6

android支持库中似乎有一个错误。

我终于找到了这个解决方法:

将布局文件更改为这个:

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

然后我通过代码 FragmentTabHost 创建了这个例子:

@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;
}
于 2014-01-20T17:07:33.520 回答
5

我遇到了这个问题,进行了研究,但找不到任何可行的解决方案。但是我注意到当我在'FragmentTabHost#setup'之后添加一个选项卡时,不会出现这个问题,但是例如,当我在'AsyncTask#onPostExecute'中添加一个选项卡时,就会出现这个问题。这似乎很愚蠢,但我试过了,它是.. 基于这些,我找到了一个解决方案:

在“FragmentTabHost#setup”之后添加一个空选项卡,然后在任何地方添加其他选项卡,我尝试了这个解决方案,它有效!我将在下面的布局和部分代码:

<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);
        }
    }
}
于 2014-03-04T15:53:15.983 回答
3

我在片段中使用 tabHost 时遇到了类似的问题,搜索了很多,最后找到了解决方案。

1)让你的代码在创建视图中

2)这是关键->在为选项卡指示器膨胀布局时,使用如下

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

我们需要将充气机中的父值设置为mTabHost.getTabWidget()

使用上述代码设置选项卡指示器解决了

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

更新 :

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);
于 2014-12-12T12:38:51.017 回答
1

当您尝试在 onViewCreated 中设置 FragmentTabHost 时会出现此问题,但为时已晚。尝试在 onCreateView 中设置它,并在返回视图对象之前添加至少一个选项卡。

于 2014-11-13T13:16:28.403 回答
0

我的开发环境是带有 API19 SDK 的 Android Studio 0.8.9。

如果我将 FragmentTabHost 放在 FragmentActivity 中,它就可以工作。当我将 FragmentTabHost 放在 Fragment 中时,它会在渲染时出现“没有已知标签为空的标签”,并在 LayoutInflater 膨胀布局时出现运行时错误。

感谢 user3216049 的回答,这是一个很好的解决方法。抱歉,我是新手,无法投票。:(

但是它在我的测试选项卡片段中没有显示任何内容。我做了一个小修改。

  • 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代码重点是我在FragmentTabHost.setup()中把id改成了“R.id.realtabcontent”

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

于 2014-10-22T10:00:31.417 回答
0

我通过引用此链接解决了我的问题

FragmentTabHost - 没有已知标签为空的标签

No tab known for tag null 它意味着 tabhost 尚未初始化,因为您正在尝试 onCreateView 方法,但为时已晚

在 onResume 方法中调用 tabhost

于 2015-12-26T06:25:25.290 回答