Tabs on Android

Asked

Viewed 573 times

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 the other with SWITCH.

I created the XML, where I can check without problems and also encoded in a correct way in Java, I believe.

After compiling, it does not even enter the app, presenting a message that it needed to be terminated. I’m already using Android Studio, is there any compatibility problem?

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>

Error below:

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.
  • Can you put which error appears in your logcat please?

  • Opa @Victor. I was able to solve...missing information spec1.setIndicator("IF");e spec2.setIndicator("SWITCH");Anyway, thanks!!!

1 answer

2


The problem in question is related to the lack of a label for the tab, as has been said here, to solve 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);
  • I use Android Studio 3.0.1 and still this answer did not solve my problem. My case is identical to Luiz Henrique, only changes the version of Android Studio. Changed the way to create tabs?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.