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

StackOverflow https://stackoverflow.com/questions/19324352

  •  30-06-2022
  •  | 
  •  

Pregunta

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

}

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: you must specify a way to create the tab content

I can't seem to find any thing on this error.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top