TabHost crash in setup

by Patrick » Tue, 13 Jan 2009 03:04:09 GMT

Sponsored Links
 I can't seem to get a TabHost to work. I am not extending TabActivity.
The tabs are part of the UI, but I don't want them to be the full
screen, but instead be part of the main layout. Think of it as in most
tabbed dialog in windows, where the OK and Cancel buttons are not
inside the tabs.

I've tried just having the TabHost inside a LinearLayout in the xml. I
get the TabHost view with findById, call setup, and crash.

I tried adding a FrameLayout manually as a child of the TabHost in the
xml, but the same thing happens.

There were other posts about this, and the answer seems to have been
"Use TabActivity". But in my case, I can't (Or at least would rather
not). I don't need to have it launch an activity per tab. I just need
it to, well, behave like tab controls do in other systems : tell me
which one is selected, and show the user only the subview associated
with the selected tab.

Is there a bug in TabHost that makes it unusable without TabActivity ?


TabHost crash in setup

by Mark Murphy » Tue, 13 Jan 2009 03:25:51 GMT


I think that is a problem.


Then don't use the built-in tabs mechanism. Use a ViewFlipper and roll
your own tabs.

Is there a bug in your car that makes in unusable without fuel?

In other words, the fact that TabHost may depend upon TabActivity is not
a bug, but rather a dependency, one that might need better documentation.

Mark Murphy (a Commons Guy) 
Android Training in Sweden -- 


Sponsored Links

TabHost crash in setup

by skink » Tue, 13 Jan 2009 03:55:04 GMT


why? if you call TabHost's setup() it works like a charm.


TabHost crash in setup

by Mark Murphy » Tue, 13 Jan 2009 04:04:59 GMT


My apologies.

Mark Murphy (a Commons Guy) 

Android Training on the Ranch! -- Mar 16-20, 2009 


TabHost crash in setup

by Patrick » Tue, 13 Jan 2009 08:20:05 GMT

 To say that TabHost not working without deriving from TabActivity is
not a bug is a bit strange. Buttons do not require the activity to
derive from ButtonActivity to not crash, do they? And it's not lilke a
car not working without fuel. It's more like a car not working because
there are other cars on the road. A tab control is not some new UI
design unique to Android that only makes sense in a very specific
context, so it's not unreasonable to expect it to work like every
other UI elements : I should be able to place one in my layout
wherever I want, or maybe even have two of them in the same layout,
and it should not crash. I should be able to get the selected tab of
each of them individually, and do whatever processing I wish based on
that information.

That aside, what skinks mention is precisely where my problem lies :
the call to setup() crashes.

I've seen another post where someone posted their XML layout, and they
had manually added a TabWidget and FrameLayout with specific android
IDs as children of the TabHost. I thought from reading the docs that
creating those was what setup was for. It seems like a really bad idea
to manually build UI elements that are OS dependent, since some future
version might have different internal requirements and the app would
no longer work.

So the question remains. Which of the following applies :
1. It is a bug (ie. Some future OS version will fix it).
2. TabHost will never work outside of TabActivity. (It should then be
removed from the classes creatable in the layout editor)
3. It works, but there is something I'm supposed to call on it/add in
the XML that I missed or isn't in the docs. (For example, it cannot be
a child of a LinearLayout)
4. Nobody knows. It works perfectly fine for other people, and I'm
just unlucky.


TabHost crash in setup

by Mark Murphy » Tue, 13 Jan 2009 11:28:52 GMT


You have a preconceived notion of how tabs should work, and Android's
tabs may not meet that notion.

Similarly, I have a preconceived notion of how a combobox should work,
and neither Spinner nor AutoCompleteTextView meet my notion. The
difference is, I don't claim Android's Spinner and AutoCompleteTextView
are buggy, just because they don't behave quite as I'd like.

You are welcome to write a replacement if the supplied one does not do
what you want, just as there is a myriad of widgets in desktop UI
frameworks that replace OS-supplied ones to overcome various perceived

For example, someday, I may craft something closer to my vision of a

And the error is...what? Post a traceback from adb logcat, DDMS, or the
Eclipse equivalent, and we may be able to help.

I confess to perhaps jumping the gun on TabHost and kin not working
outside TabActivity. That is definitely the case for
MapView/MapActivity, and I may have drawn an incorrect parallel. Being
short on sleep ain't helping my cognitive faculties... :-(

So, give us some exception information, and let's see if we can solve
your problem.

Mark Murphy (a Commons Guy) 
_The Busy Coder's Guide to Android Development_ Version 2.0 Published!


TabHost crash in setup

by Patrick » Wed, 14 Jan 2009 03:15:36 GMT

 Well that will teach me. LogCat did have the answer, but the window
was too small and it clipped the important part of the exception
message, and I only saw it when copy-pasting it here :

Caused by: java.lang.RuntimeException: Your TabHost must have a
TabWidget whose id attribute is ''

So I guess I am supposed to manually create the two children with
specific IDs. Something to be added to the docs for the next revision
I guess :)

So out of curiosity, if it doesn't create the children views, anyone
know what the call to setup() does?


TabHost crash in setup

by Mark Murphy » Wed, 14 Jan 2009 03:22:56 GMT

 > So out of curiosity, if it doesn't create the children views, anyone

Here's the TabHost source code: 

Mostly, setup() seems to hook up to various events.

Mark Murphy (a Commons Guy) 
_The Busy Coder's Guide to Android Development_ Version 2.0 Available!


Other Threads

1. Adding Additional Functionality to 'In-call' screen.

Hi, Group !

I am planning to add additional functionality to existent 'In-call'
screen at bottom empty space. As far as I know, we do not have source
available for CALL_PHONE.  So, what are my possible alternatives?

Do I have to write my own Dialer App? Or If the CALL_PHONE source is
available, where would I find it?


Dipen Dalal
Foxconn, Inc.


2. Media Player suggestion - Don't add ringtones to random playlists

I have a suggestion.

I will admit this might have already have been taken care of but- if
not I'm hoping- it could be considered before the first android phone

One thing that annoys the heck out of me on my current Sony phone is
the media player automatically imports both ringtones and- mp3s for a
playlists.  I hope--hope--hope-- that Android will have in it a
"Music" folder and a separate "Ringtones" folder.  And hopefully the
MediaPlayer will be programmed in such a way that the media player on
the phone wont just take all the ringtones and add them to the media
player's playlist.  It is sooooooo annoying to be listening to a
random playlist during my morning commute then all of a sudden the
default phone ringtones and a bunch of other ringtones start cutting
into the playlist and I have to take the time to keep pressing
forward...  It is my hope that Android will notice little flaws like
these and will have it taken care of in the first version.

~J. Payne

3. R cannot be resolved!

4. Not Able to get self presence and self custom message

5. New gmail account cannot use gtalk service?

6. Can I sync with Voxmobili syncml server?

7. Energy-aware applications