【问题标题】:Android FragmentTabHost : No tab known for tag nullAndroid FragmentTabHost:没有标签为空的标签
【发布时间】:2015-12-26 14:25:25
【问题描述】:

我使用了下面的代码,它不是渲染图形布局。将错误显示为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>

【问题讨论】:

标签: android android-fragments


【解决方案1】:

这是我用来初始化 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
            }
        }
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,进行了研究,但找不到任何可行的解决方案。但是我注意到当我在'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);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

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

        1) 在创建视图中保留代码

        2) 这是关键 -> 为标签指示器的布局膨胀时,使用如下

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

        我们需要将inflater中的父值设置为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);
        

        【讨论】:

        • 请添加一些示例代码,我应该在哪里使用这个tabIndicator
        • 更新了我的答案请查看
        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

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

          如果我将 FragmentTabHost 放在 FragmentActivity 中,它就可以工作。 当我将 FragmentTabHost 放在 Fragment 中时,它会在渲染时出现“没有已知标签为 null 的标签”,并在 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;
                  }
              }
          }
          

          【讨论】:

          • 如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。
          • 您的意思是我创建了一个新问题并将我的答案放在那里?然后在这里放一个链接到我的新问题?实际上,我的问题与原始帖子相同。我只是修改了一个答案并修复了它以获得正确的结果。谢谢。
          【解决方案7】:

          我通过参考这个链接解决了我的问题

          FragmentTabHost - No tab known for tag null

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

          在 onResume 方法中调用 tabhost

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-25
            相关资源
            最近更新 更多