简体   繁体   English

ActionBar不适用于api级别<14

[英]ActionBar doesn't work with api level < 14

In my application, i use ViewPager with ActionBar which require api level > 11 . 在我的应用程序中,我将ViewPager与ActionBar一起使用,这需要api level> 11。 But it doesn't work for me with api level 11 , 12 and 13. 但这对于api级别11,12和13的我来说不起作用。

Errors come from this line ( 22 ): 错误来自此行(22):

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

And the error is : 错误是:

11-17 14:34:41.795: E/AndroidRuntime(472): Caused by: java.lang.NullPointerException
11-17 14:34:41.795: E/AndroidRuntime(472):  at fr.carnet.free.ViewPagerActivity.onCreate(ViewPagerActivity.java:22)

Other code : 其他代码:

// Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }

Thanks 谢谢

I'm assuming that you're calling getActionBar() to obtain your action bar. 我假设您正在调用getActionBar()来获取操作栏。 The documentation for getActionBar() clearly states: getActionBar()的文档明确指出:

The Activity's ActionBar, or null if it does not have one. 活动的ActionBar;如果没有,则为null。

Also, the documentation of ActionBar states: 此外, ActionBar的文档指出:

Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default. 从Android 3.0(API级别11)开始,当活动使用系统的Holo主题 (或其子主题之一)时,操作栏将显示在活动窗口的顶部。 You may otherwise add the action bar by calling requestFeature(FEATURE_ACTION_BAR) or by declaring it in a custom theme with the windowActionBar property. 否则,您可以通过调用requestFeature(FEATURE_ACTION_BAR)或通过使用windowActionBar属性在自定义主题中对其进行声明来添加操作栏。

So you might want to call requestFeature(FEATURE_ACTION_BAR) in order to set an ActionBar to your activity if you're not using one of the Holo themes for your app. 因此,如果您不为应用程序使用Holo主题之一,则可能需要调用requestFeature(FEATURE_ACTION_BAR)以便将ActionBar设置为活动。

The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above. ActionBar API最初是在Android 3.0(API级别11)中添加的,但在支持库中也可以使用,以与Android 2.1(API级别7)及更高版本兼容。

http://developer.android.com/guide/topics/ui/actionbar.html http://developer.android.com/guide/topics/ui/actionbar.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM