Eugene Shmorgun
Eugene Shmorgun

Reputation: 2075

Exception with Tabs: "you must specify a way to create tab indicator"

My application consists three tabs and two of them have the ListView and one is just activity with TextView. To get main activity with Tabs I use the example from developers.anroid.com http://developer.android.com/resources/tutorials/views/hello-tabwidget.html According to this example my layout is:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>


</LinearLayout>

</TabHost>

So, next code is code of activity (there are only two pages):

public class MainActivity extends TabActivity {



@Override
public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_layout_start_page);

    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent(this, ListViewTab1Activity.class);
    spec = tabHost.newTabSpec("Page1").setIndicator("Page 1", res.getDrawable(R.drawable.icon))
    .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent(this, ListViewTab2Activity.class);
    spec = tabHost.newTabSpec("Page 2").setIndicator("Page 2", res.getDrawable(R.drawable.icon))
    .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(1);

}

}

Every intent is pointed to another activity, which has hard-coded ListView in layout and code to define it from string-array from resources. But after starting I get exception IllegalArgument Exception ""you must specify a way to create tab indicator". and it makes me stopped, please, help me. thanks for everyone

Upvotes: 0

Views: 12520

Answers (2)

DCorrigan
DCorrigan

Reputation: 676

I just had this same problem.

I found my problem was the reuse of sp

I did this to solve the problem:

TabSpec spec1=tabHost.newTabSpec("Tab 1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.icon_tab1_config));

TabSpec spec2=tabHost.newTabSpec("Tab 2");
    spec2.setIndicator("Tab 2",getResources().getDrawable(R.drawable.icon_tab2_config));
    spec2.setContent(R.id.tab2);

using different TabSpec values fixed the issue for me.

Upvotes: 1

narancs
narancs

Reputation: 5294

    Intent i;
    i = new Intent().setClass(this, Tab1.class);
    TabSpec sp = th.newTabSpec("Tab1");
    sp.setIndicator("alma");
    sp.setContent(i);
    th.addTab(sp);

    i = new Intent().setClass(this, Tab2.class);
    sp = th.newTabSpec("Tab2");
    sp.setIndicator("alma2");
    th.addTab(sp);
    th.setCurrentTab(0);

This code is works perfectly, so you should try to make this

spec = tabHost.newTabSpec("Page 2").setIndicator("Page 2", res.getDrawable(R.drawable.icon))

get separated as I had done. Maybe shoudn`t use single command line to set attrs.

Upvotes: 1

Related Questions