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

前端 未结 1 1258
没有蜡笔的小新
没有蜡笔的小新 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

    
    
    
        
    
            
        
    
    

    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)
提交回复
热议问题