The graphical layout for a simple android.support.v4.app.FragmentTabHost never renders in either Eclipse or Android Studio.
The Console error I get is consistently:Exception raised during rendering: No tab known for tag null
一个简单的android.support.v4.app的图形布局。FragmentTabHost不会在Eclipse或Android Studio中呈现。我得到的控制台错误是一致的:呈现期间引发的异常:没有标记为null的选项卡
I'm using the most basic XML file:
我使用的是最基本的XML文件:
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<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>
but the same error occurs.
但同样的错误发生了。
I just wanted to add more views above or below the tab widget and frame layout.
I don't care so much about seeing the tab content; I just want to see the rest of my layout - but the problem is that NO OTHER VIEWS are rendered when a android.support.v4.app.FragmentTabHost
resides in the layout.
我只是想在选项卡小部件和框架布局上下添加更多的视图。我不太关心看到标签的内容;我只是想看看我的布局的其余部分——但问题是当android.support.v4.app时,没有显示其他视图。FragmentTabHost驻留在布局中。
I've read and tried to resolve the issue from the answer to this post:
Android: Tabs at the bottom with FragmentTabHost
but I don't think that that is my problem; I'm not looking to put a TabWidget on the bottom.
我已经阅读并试图从这篇文章的答案中解决这个问题:Android:底部有FragmentTabHost的标签,但我不认为这是我的问题;我不想在底部放一个TabWidget。
Every other one of my XML files opens perfectly.
我的每一个XML文件都能完美地打开。
The same problem occurs in Android Studio:
同样的问题也出现在Android Studio中:
5 个解决方案
#1
4
I had the same rendering problem as well as compilation error. I fixed the problem by finding that I was not passing Fragment when i was creating Addtab. You must pass atleast one fragment on mTabHost.addTab. Below is the working code.
我有同样的渲染问题和编译错误。我发现在创建Addtab时没有传递片段,从而解决了这个问题。您必须在mTabHost.addTab上传递至少一个片段。下面是工作代码。
private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(HomeActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"), HomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mysheets").setIndicator("MySheets"));
mTabHost.addTab(mTabHost.newTabSpec("bookmarks").setIndicator("Bookmarks"));
#2
1
Not sure about the error you've got (sorry, I'm really busy right now so can't spend more time checking) but in general it seems that the FragmentTabHost
from the support libs doesn't care about the xml at all. See my previous answer to another question:
不确定您所得到的错误(抱歉,我现在真的很忙,所以不能花更多时间检查),但是总的来说,来自support libs的FragmentTabHost似乎根本不关心xml。请看我之前对另一个问题的回答:
FragmentTabHost with horizontal scroll
FragmentTabHost与水平滚动
#3
1
From Layout i am getting the same Error..so,I resolve that Problem by Code only...It's working fine..Please try this code
从布局上看,我也有同样的错误。所以,我只用代码来解决这个问题……这是工作正常。请尝试这个代码
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
}
}
}
}
#4
1
If U need to put fragmented tabs at bottom of screen ... @fallow undermentioned --
如果你需要在屏幕底部放置碎片……@fallow下述的——
Make your xml file like this ..
让您的xml文件像这样。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"> android:layout_alignParentTop="true" -->
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Now if your concern is opening several fragments with in single fragmented tabs ...
现在,如果您的关注点是用单个片段标签打开几个片段……
@follow steps ::
@follow步骤::
- Create a container fragment. This container fragment will be default for all of your's tabs content.
- 创建一个容器片段。此容器片段将默认用于所有的选项卡内容。
- For every tab content replace fragment U need with this container.
- 对于每个选项卡内容,用这个容器替换您需要的片段。
For ex:- Just like you replace your bed with different bed sheets .. :)
就像你用不同的床单代替你的床一样:)
Your container fragment class that will be used differently in different tabs ... "LearnContainerFragment.java "
您的容器片段类将在不同的选项卡中使用不同的……“LearnContainerFragment。java”
public class LearnContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
}
}
private void initView() {
Log.e("test", "tab 1 init view");
replaceFragment(new Learn(), false);
}
}
LearnContainerFragment.java --- > it's xml file container_fragment.xml
LearnContainerFragment。它是xml文件container_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
@ How to use Conatiner..
@如何使用Conatiner。
- For every fragment U need will be replaced with id of this container fragment.
- 对于您需要的每个片段,都将被替换为该容器片段的id。
@last your BaseContainerFragment.java class --
@last BaseContainerFragment。java类。
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
Hope it helps..... Cheers!
希望它能帮助.....干杯!
#5
0
not sure.... but shouldn't your layout have a tabhost tag in it above the tabwidget's linear layout?
不确定....但是,难道您的布局不应该在tabwidget的线性布局之上设置一个tabhost标记吗?
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
>
i made an app a while back that implented tabs using tabhost and this is how my layout was...one tab had a calendar view one had an image switcher and one had a listview...sorry i can't be of more help
我不久前做了一个应用,用tabhost实现标签,这就是我的布局……一个标签有日历视图,一个有图像切换器,一个有listview…对不起,我帮不了你
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="251dp" >
</ImageSwitcher>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
#1
4
I had the same rendering problem as well as compilation error. I fixed the problem by finding that I was not passing Fragment when i was creating Addtab. You must pass atleast one fragment on mTabHost.addTab. Below is the working code.
我有同样的渲染问题和编译错误。我发现在创建Addtab时没有传递片段,从而解决了这个问题。您必须在mTabHost.addTab上传递至少一个片段。下面是工作代码。
private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(HomeActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"), HomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mysheets").setIndicator("MySheets"));
mTabHost.addTab(mTabHost.newTabSpec("bookmarks").setIndicator("Bookmarks"));
#2
1
Not sure about the error you've got (sorry, I'm really busy right now so can't spend more time checking) but in general it seems that the FragmentTabHost
from the support libs doesn't care about the xml at all. See my previous answer to another question:
不确定您所得到的错误(抱歉,我现在真的很忙,所以不能花更多时间检查),但是总的来说,来自support libs的FragmentTabHost似乎根本不关心xml。请看我之前对另一个问题的回答:
FragmentTabHost with horizontal scroll
FragmentTabHost与水平滚动
#3
1
From Layout i am getting the same Error..so,I resolve that Problem by Code only...It's working fine..Please try this code
从布局上看,我也有同样的错误。所以,我只用代码来解决这个问题……这是工作正常。请尝试这个代码
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
}
}
}
}
#4
1
If U need to put fragmented tabs at bottom of screen ... @fallow undermentioned --
如果你需要在屏幕底部放置碎片……@fallow下述的——
Make your xml file like this ..
让您的xml文件像这样。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"> android:layout_alignParentTop="true" -->
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Now if your concern is opening several fragments with in single fragmented tabs ...
现在,如果您的关注点是用单个片段标签打开几个片段……
@follow steps ::
@follow步骤::
- Create a container fragment. This container fragment will be default for all of your's tabs content.
- 创建一个容器片段。此容器片段将默认用于所有的选项卡内容。
- For every tab content replace fragment U need with this container.
- 对于每个选项卡内容,用这个容器替换您需要的片段。
For ex:- Just like you replace your bed with different bed sheets .. :)
就像你用不同的床单代替你的床一样:)
Your container fragment class that will be used differently in different tabs ... "LearnContainerFragment.java "
您的容器片段类将在不同的选项卡中使用不同的……“LearnContainerFragment。java”
public class LearnContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
}
}
private void initView() {
Log.e("test", "tab 1 init view");
replaceFragment(new Learn(), false);
}
}
LearnContainerFragment.java --- > it's xml file container_fragment.xml
LearnContainerFragment。它是xml文件container_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
@ How to use Conatiner..
@如何使用Conatiner。
- For every fragment U need will be replaced with id of this container fragment.
- 对于您需要的每个片段,都将被替换为该容器片段的id。
@last your BaseContainerFragment.java class --
@last BaseContainerFragment。java类。
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
Hope it helps..... Cheers!
希望它能帮助.....干杯!
#5
0
not sure.... but shouldn't your layout have a tabhost tag in it above the tabwidget's linear layout?
不确定....但是,难道您的布局不应该在tabwidget的线性布局之上设置一个tabhost标记吗?
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
>
i made an app a while back that implented tabs using tabhost and this is how my layout was...one tab had a calendar view one had an image switcher and one had a listview...sorry i can't be of more help
我不久前做了一个应用,用tabhost实现标签,这就是我的布局……一个标签有日历视图,一个有图像切换器,一个有listview…对不起,我帮不了你
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="251dp" >
</ImageSwitcher>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>