I have an issue I have not been able to figure out and it is only happening on devices running <2.1. It works fine on android 2.2. I have ansynchronous task that displa
An easy solution is to set the tabWidget visibility to gone in your layout
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" />
Then once you have defined your tabs content, you can make it visible again:
// Add tab content (here a fragment class)
tabHost.addTab(
tabHost.newTabSpec("tag1").setIndicator("Title"),
contentFragment.class,
null);
// Set tabWidget visible again
tabWidget.setVisibility(View.VISIBLE);
Had the same problem with tabs populated from AsyncTask on 1.6 and 2.1. Looks like earlier versions doesn't like TabHost without any tabs. To solve it, I do not use TabActivity and create TabHost with all its hierarchy manually in onPostExecute function of AsyncTask.
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
Are you allowed to use the same 'tag' for more than one TabSpec? I'd try setting those correctly and see if it fixes it.
EDIT: OK, so my suggestion didn't fix it but it makes sense to have unique tags anyway.
Try this to see if it helps. Add an option to set the currently selected tab at the end of your LoadLayout() method like so (see last line)...
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
tabHost.setCurrentTab(0); // <== Add this
EDIT2: I found the TabWidget.java source and line 206 (where the NullPointerException occurs) is...
mBottomLeftStrip.setState(selectedChild.getDrawableState());
...there are three possible causes of the exception that I can see.
selectedChild.getDrawableState()
is nullThe last seems to be the likely cause but I'm not sure what could cause it to return null.
Try Google for 'TabWidget.java source' - the second result points at grepcode.com which has line numbers and you can see what it's trying to do at the point of the exception.
Inspired by one of related TabHost answers:
public class FixedTabHost extends FragmentTabHost
{
public FixedTabHost(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void dispatchDraw(Canvas canvas)
{
try { super.dispatchDraw(canvas); }
catch (Exception ignored) {}
}
}
Worked for me.