Mark James
Mark James

Reputation: 481

Android Tab Issue throwing up runtime (looks fine to me though)

Tabhost throwing up a runtime error - code seems fine to me - but obviously not. I have 2 activities that I want to show in some tabs.

Error

(java.lang.IllegalArgumentException: you must specify a way to create the tab content)

MainActivity

public class MainActivity extends TabActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TabHost tabHost = getTabHost();


        // Tab 1
        TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
        // setting Title and Icon for the Tab

        tab1.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
        Intent tab1Intent = new Intent(this, Tab1Activity.class);
        tab1Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tab1.setContent(tab1Intent);


        // Tab 2
        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
        // setting Title and Icon for the Tab

        tab2.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
        Intent tab2Intent = new Intent(this, Tab2Activity.class);
        tab1Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tab1.setContent(tab2Intent);


        tabHost.addTab(tab1);
        tabHost.addTab(tab2);


    }
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/LinearLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              tools:context="com.example.dawnlp.innertab.MainActivity"
              android:background="#00547d"
              android:textColor="#FFFFFF"

    >



    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"


        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"

            >

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

                >

            </TabWidget>

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

                >


            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

Dont think it has anything to do with the other activities I have.

Upvotes: 0

Views: 21

Answers (1)

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 4353

change last tab code as : You are using tab1 instead of tab2 for indicator

 // Tab 2
        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
        // setting Title and Icon for the Tab

        tab2.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
        Intent tab2Intent = new Intent(this, Tab2Activity.class);
        tab2Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tab2.setContent(tab2Intent);

Upvotes: 1

Related Questions