Tabs on Android

2

I'm trying to implement a simple code just to show two tabs on Android and later add a content dividing into two activities, one to show examples with IF and another with SWITCH.

I created the XML, where I can check without problems and I also coded correctly in Java, I think.

After compiling, it does not even enter the application, displaying a message that it needs to be terminated. I'm already using Android Studio, is there a compatibility issue?

Below my Java code:

public class MainActivity extends ActionBarActivity {

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

        TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1 = tabHost.newTabSpec("IF");
        spec1.setContent(R.id.IF);

        TabSpec spec2 = tabHost.newTabSpec("SWITCH");
        spec2.setContent(R.id.SWITCH);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);

    }
}

Below XML:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost"
    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
            android:id="@+id/IF"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="60px">

            <TextView
                android:id="@+id/txt1"
                android:layout_width="fill_parent"
                android:layout_height="100px"
                android:text="Esta e a aba 01" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/SWITCH"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingTop="60px">

            <TextView
                android:id="@+id/txt2"
                android:layout_width="fill_parent"
                android:layout_height="100px"
                android:text="Esta e a aba 2" />

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

Below error:

12-27 01:24:29.300  30658-30658/com.example.luizhmu.aulas_android_if_switch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.luizhmu.aulas_android_if_switch, PID: 30658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luizhmu.aulas_android_if_switch/com.example.luizhmu.aulas_android_if_switch.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
    
asked by anonymous 27.12.2014 / 02:07

1 answer

2

The problem in question is related to the lack of a label for the tab, as spoken here , to resolve it do the following:

TabHost.TabSpec spec1=tabHost.newTabSpec("IF");
spec1.setContent(R.id.IF);
spec1.setIndicator("rótulo 1"); // <-----

TabHost.TabSpec spec2=tabHost.newTabSpec("SWITCH");
spec2.setContent(R.id.SWITCH);
spec2.setIndicator("rótulo 2"); // <-----

tabHost.addTab(spec1);
tabHost.addTab(spec2);
    
27.12.2014 / 14:12