2013-03-12 78 views
43

我使用下面的代码,它不是渲染图形布局。显示错误为Exception raised during rendering: No tab known for tag nullAndroid的FragmentTabHost:没有选项卡已知的标记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> 
+0

你使用什么代码?你在使用菜单xml吗?你有一个logcat吗? – 2013-07-04 05:58:27

+1

你可以发布初始化TabHost的代码吗? – 2013-07-06 04:40:46

+3

我打开了一个错误报告,请对其进行投票:https://code.google.com/p/android/issues/detail?id=78772 – Gavriel 2014-11-05 21:37:31

回答

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 
      } 
     } 
    } 
} 
+0

非常感谢! – gc986 2016-06-16 11:11:55

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

无变化........ – evya 2014-11-09 08:50:45

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

我的开发环境是安卓0.8.9工作室与API19 SDK。

如果我把FragmentTabHost放在FragmentActivity中,它就可以工作。 当我把FragmentTabHost放在一个片段中时,它在渲染时会得到“没有标签为空的标签”,并在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代码 的一点是,我改变ID在FragmentTabHost.setup了 “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; 
     } 
    } 
} 

+0

如果您有新问题,请点击[提问](http://stackoverflow.com/questions/ask)按钮。如果有助于提供上下文,请包含此问题的链接。 – talonmies 2014-10-22 10:37:02

+0

您的意思是我创建了一个新问题并将答案放在那里?然后在这里提出一个链接到我的新问题?其实,我的问题与这篇原文一样。我只是修改了一个答案并修正了它以获得正确的结果。谢谢。 – Jones 2014-10-23 02:50:42

1

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

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 
0问题

更新:

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); 
+0

请添加一些示例代码,我应该在哪里使用此tabIndicator – 2015-06-30 08:54:23

+0

更新我的答案请检查它 – 2015-06-30 09:23:10

0

我通过闯民宅此链接

FragmentTabHost - No tab known for tag null

没有选项卡标签空知的手段tabhost尚未初始化解决我的问题becouse您在onCreateView方法是trrying太晚

在onResume方法中调用tabhost

相关问题