Search code examples
androidandroid-layoutactionbarsherlock

Android getSupportActionbar() is Null when Activity is called from tabhost


I have a support action bar that works fine if I trigger the event with an intent manually. But if I leave it up to the tabhost to call it then the actionbar returned from getSupportActionbar() is null.

I've heard this referenced on Stack in another question, but no one has supplied and answer. (Apparently it only occurs on Android 3 and above). Does anyone have any ideas?

My tabhost:

public class NavTab extends TabActivity {

    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Initialize a TabSpec for each tab and add it to the TabHost
        intent = new Intent().setClass(this, SummaryPage.class);
        spec = tabHost.newTabSpec("Summary");
        spec.setIndicator("Account", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        //Feedback
        intent = new Intent().setClass(this, FeedbackPage.class);
        spec = tabHost.newTabSpec("Feedback");
        spec.setIndicator("Feedback", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        //Payment Locations
        intent = new Intent().setClass(this, PaymentLocationsActivity.class);
        spec = tabHost.newTabSpec("Payment Locations");
        spec.setIndicator("Pay Loc", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        //Usage Alert
        intent = new Intent().setClass(this, UsageAlertPage.class);
        spec = tabHost.newTabSpec("Usage Alerts");
        spec.setIndicator("Alerts", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);

    }


}

My activity

public class PageWithActionBar extends SherlockActivity implements ActionBar.OnNavigationListener {

    private static String TAG = "mymeter-Main";
    private List<Account> accounts = new LinkedList<Account>();
    private LocationAdapter locationAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setDisplayShowTitleEnabled(false);

        accounts.add(new Account("123456789", "4-15 Rose Rd", "Auckland 1021"));
        accounts.add(new Account("0987654321", "49 Ronaki Rd", "Auckland 1043"));
        locationAdapter = new LocationAdapter(this, accounts);
        actionBar.setListNavigationCallbacks(locationAdapter, this);


    }
}

Solution

  • Your TabActivity needs to extend SherlockActivity, check out the ActionBarSherlock samples on Tabs