FragmentTabHost 图形布局不呈现 [英] FragmentTabHost graphical layout doesn't render
问题描述
简单的 android.support.v4.app.FragmentTabHost 的图形布局永远不会在 Eclipse 或 Android Studio 中呈现.
我得到的控制台错误始终如一:渲染过程中引发的异常:没有已知标签为空的标签
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
我使用的是最基本的 XML 文件:
I'm using the most basic XML file:
<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>
但是出现同样的错误.
我只想在标签小部件和框架布局的上方或下方添加更多视图.
我不太在意看到标签内容;我只想查看我的其余布局 - 但问题是当 android.support.v4.app.FragmentTabHost
驻留在布局中.
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:带有 FragmentTabHost 的底部标签
但我不认为那是我的问题;我不想在底部放置 TabWidget.
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.
我的每个其他 XML 文件都可以完美打开.
Every other one of my XML files opens perfectly.
Android Studio 也出现同样的问题:
The same problem occurs in Android Studio:
推荐答案
我遇到了同样的渲染问题以及编译错误.我发现我在创建 Addtab 时没有传递 Fragment,从而解决了这个问题.您必须在 mTabHost.addTab 上传递至少一个片段.下面是工作代码.
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.
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"));
这篇关于FragmentTabHost 图形布局不呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!