从tabhost调用Activity时,Android getSupportActionbar()为Null

时间:2012-11-19 03:44:50

标签: android android-layout actionbarsherlock

如果我手动触发事件,我有一个支持操作栏可以正常工作。但是如果我把它留给tabhost来调用它,那么从getSupportActionbar()返回的操作栏就是null。

我在另一个问题上听到过Stack上的引用,但没有人提供和回答。 (显然它只发生在Android 3及更高版本上)。有没有人有任何想法?

我的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);

    }


}

我的活动

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);


    }
}

1 个答案:

答案 0 :(得分:2)

您的TabActivity需要扩展SherlockActivity,请查看选项卡上的ActionBarSherlock示例