编程语言
首页 > 编程语言> > Android:FragmentTabHost-java.lang.IllegalArgumentException:您必须指定一种创建标签内容的方法

Android:FragmentTabHost-java.lang.IllegalArgumentException:您必须指定一种创建标签内容的方法

作者:互联网

我在用于创建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内部,我发现以下错误:

java.lang.IllegalArgumentException:您必须指定一种创建选项卡内容的方法

我似乎找不到关于此错误的任何信息.

解决方法:

无法看到您的xml文件(R.layout.tabs),我会说某些设置不正确.

代替:

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并像这样扩展您的资源管理器选项卡的布局.

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

我没有尝试在选项卡上合并图像,但是此代码对我有用.让我知道是否有帮助.

标签:android,fragment-tab-host
来源: https://codeday.me/bug/20191011/1893114.html