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

前端 未结 1 1259
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 16:07

I have the following in a class for creating a FragmentTabHost:

public class TabsActivity extends FragmentActivity {
private FragmentTabHost mTabHos         


        
相关标签:
1条回答
  • 2021-01-22 16:39

    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.

    0 讨论(0)
提交回复
热议问题