安卓:FragmentTabHost - java.lang.IllegalArgumentException:如果你必须指定一个方法来创建选项卡内容 [英] Android: FragmentTabHost - java.lang.IllegalArgumentException: you must specify a way to create the tab content
问题描述
我有以下的类来创建一个 FragmentTabHost
:
I have the following in a class for creating a FragmentTabHost
:
public class TabsActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
TabHost.TabSpec exploreSpec = mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon));
mTabHost.addTab(exploreSpec);
}
}
我有一个单独的.xml文件,设置主机如在Android支持网站。这一切都是在第二个活动。我的主要活动有一组图像。单击某个加载此活动。该应用程序运行初期。当我点击一个图像加载此活动,虽然它崩溃。里面LogCat中,我发现了以下错误:
I have a separate .xml file for setting up the host as shown in the android support site. This is all in a second activity. My primary activity has a group of images. Clicking one loads this activity. The app runs initially. As soon as I tap an image to load this activity though it crashes. Inside LogCat I found the following error:
java.lang.IllegalArgumentException:如果你必须指定一个方法来创建选项卡内容
我似乎无法找到关于此错误的任何事情。
I can't seem to find any thing on this error.
推荐答案
而不能看到你的XML文件(R.layout.tabs)我会说一些没有设置完全正确。
Without being able to see your xml file (R.layout.tabs) I'd say something is not set up quite right.
相反的:
TabHost.TabSpec exploreSpec = mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon));
mTabHost.addTab(exploreSpec);
尝试:
mTabHost.addTab(mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon)), TheAssociatedFragmentTab.class, null);
R.layout.tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
除此之外,你只需要一个ExplorerFragment类(扩展片段),它重写onCreateView和膨胀的布局,像这样的浏览器选项卡。
Beyond that you will just need an ExplorerFragment class (extends fragment) which Overrides onCreateView and inflates the layout for your explorer tab like so..
ExplorerFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.explorer_fragment, container, false);
}
我并没有试图将一个图像在我的标签,但这code对我的作品。让我知道有没有什么帮助。
I didn't try to incorporate an image on my tabs, but this code works for me. Let me know if that helps.
这篇关于安卓:FragmentTabHost - java.lang.IllegalArgumentException:如果你必须指定一个方法来创建选项卡内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!