Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TabHost Example

I did and run example. But i take error.

Error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabmenu/com.example.tabmenu.MainActivity: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.

mainActivity.java

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabSpec tab1 = tabHost.newTabSpec("First Tab");
        TabSpec tab2 = tabHost.newTabSpec("Second Tab");
        TabSpec tab3 = tabHost.newTabSpec("Third Tab");

        tab1.setIndicator("Tab1");
        tab1.setContent(new Intent(this,Tab1Activity.class));

        tab1.setIndicator("Tab2");
        tab1.setContent(new Intent(this,Tab2Activity.class));

        tab1.setIndicator("Tab3");
        tab1.setContent(new Intent(this,Tab3Activity.class));

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

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tabmenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity 
            android:name="com.example.tabmenu.Tab1Activity"/>
        <activity
            android:name="com.example.tabmenu.Tab2Activity"/>
        <activity
            android:name="com.example.tabmenu.Tab3Activity"/>
        <activity
            android:name="com.example.tabmenu.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
like image 350
c.mert Avatar asked Feb 20 '26 14:02

c.mert


1 Answers

try to change this segment:

tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));

tab1.setIndicator("Tab2");
tab1.setContent(new Intent(this,Tab2Activity.class));

tab1.setIndicator("Tab3");
tab1.setContent(new Intent(this,Tab3Activity.class));

with the below one .... like this:

tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));

--> tab2.setIndicator("Tab2");
--> tab2.setContent(new Intent(this,Tab2Activity.class));

--> tab3.setIndicator("Tab3");
--> tab3.setContent(new Intent(this,Tab3Activity.class));

The error is because your are not using tab2 or tab3 and using tab1 repeatedly.

Hope this helps.

like image 83
Laura Pulido Avatar answered Feb 22 '26 02:02

Laura Pulido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!