Android: FragmentTabHost - java.lang.IllegalArgumentException: you must specify a way to create the tab content

会有一股神秘感。 提交于 2019-12-02 01:32:23

Without being able to see your xml file (R.layout.tabs) I'd say something is not set up quite right.

Instead of:

TabHost.TabSpec exploreSpec = mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon));
    mTabHost.addTab(exploreSpec);

Try:

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>

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

I didn't try to incorporate an image on my tabs, but this code works for me. Let me know if that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!