Issues with Android TabHost Example
Asked Answered
A

4

8

I have been attempting to implement the 'advanced' tabwidget example from google. But, when it tries to call tabHost.addTab(spec); I get a stack trace from the debugger. Sorry, I don't have the stack trace here, but I'm wondering if others have had this same issue (as this code had a number of typo's and missing information that stopped me from even compiling.

Can anyone point me to a corrected/running version of this code?

The updated information needed are:

<activity android:name=".ArtistsActivity"></activity>
<activity android:name=".AlbumsActivity"></activity>
<activity android:name=".SongsActivity"></activity>
Abscess answered 5/2, 2010 at 18:26 Comment(2)
I followed that example just last night and it worked fine. I didn't copy-paste the code out though, so it may be buggy. Get us your stack trace and we can help.Charmer
While a new Android developer probably will need the syntax as spelled out here since it doesn't come up in any prior tutorial, they appear to have updated the tutorial to briefly mention this requirement in step 2: "Duplicate this for each of the three activities, and add the corresponding <activity/> tags to the Android Manifest file" (20 Dec. 2010). If anyone needs it, the full activity syntax reference is available on developer.android.com.Fromm
T
9

The current TabHost Example on the Android Developers site contains one error and also leaves out an important step that will prevent the example from running.

First off: In the onCreate() method that is added to HelloTabWidget class attempts to use a TabHost object called mTabHost. This is invalid, it should be tabHost.

Second: The tutorial leaves out the details that you need to add each of the activities too the AndroidManifest.xml. Without this the code will not work and you will get "force close" each time you attempt to execute.

Tautog answered 8/2, 2010 at 4:44 Comment(3)
I saw that alluded to elsewhere on another site. I'll track it down, modify and accept when I've verified. Thx.Abscess
The 'elsewhere' is code.google.com/p/android/issues/detail?id=4183, but I haven't checked it yet. Tonight, I hope.Abscess
@GrandPrix: OMG I wish Google would fix their examples. They are still wrong! And I had the exact same problem and it was driving me insane today. Thank you SO much for this answer. :)Junejuneau
G
12

I spent the last hour or so going through that tutorial. Here's the problems and fixes for it that I dealt with:

Step 2: When creating your activities, if you do not create them through the manifest then you'll need to add them to the manifest manually.

Add these lines to AndroidManifest.xml:

  <activity android:name=".AlbumsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
        </activity>
  <activity android:name=".ArtistsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
        </activity>
          <activity android:name=".SongsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
        </activity>

Step 3: You are only instructed to create the ic_tab_artists.xml file. You'll need to create one for ic_tab_songs.xml and ic_tab_albums.xml as well. You can just duplicate the ic_tab_artists.xml (or change the HelloTabView.java tab specs to use the artists.xml file for each tab).

Step 4: The third to last line under /res/layout/main has a typo (a ; instead of a :)

      android:padding="5dp" />
    </LinearLayout>
</TabHost>

Step 6: There is a typo that uses calls mTabHost instead of tabHost. Change it.

As already cited the getIntent() function on the last line isn't appropriate. I just call the tab based on it's id. eg:

tabHost.setCurrentTabByTag("albums");
Garonne answered 11/2, 2010 at 5:7 Comment(0)
T
9

The current TabHost Example on the Android Developers site contains one error and also leaves out an important step that will prevent the example from running.

First off: In the onCreate() method that is added to HelloTabWidget class attempts to use a TabHost object called mTabHost. This is invalid, it should be tabHost.

Second: The tutorial leaves out the details that you need to add each of the activities too the AndroidManifest.xml. Without this the code will not work and you will get "force close" each time you attempt to execute.

Tautog answered 8/2, 2010 at 4:44 Comment(3)
I saw that alluded to elsewhere on another site. I'll track it down, modify and accept when I've verified. Thx.Abscess
The 'elsewhere' is code.google.com/p/android/issues/detail?id=4183, but I haven't checked it yet. Tonight, I hope.Abscess
@GrandPrix: OMG I wish Google would fix their examples. They are still wrong! And I had the exact same problem and it was driving me insane today. Thank you SO much for this answer. :)Junejuneau
F
2

Also the

android:theme="@android:style/Theme.NoTitleBar"

does not work for me, if I replace it with

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

it works fine.

Farrica answered 21/3, 2010 at 9:41 Comment(0)
C
1

The example, as corrected by Ted, also works if all three activities do not include the line:

android:theme="@android:style/Theme.NoTitleBar"

This is useful if you want a title bar in addition to the tab labels.

Coniah answered 22/4, 2010 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.