android.app.Actionbar.setHomeButtonEnabled returns 空

android.app.Actionbar.setHomeButtonEnabled returns null

我正在尝试在 android 中创建可滑动的标签。甚至在 activity 启动应用程序崩溃之前就说 setHomeButtonenabled 返回空指针异常。

我的 onCreate 方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });


}

这是我的 LOGCAT:

    03-09 22:38:24.674: E/AndroidRuntime(1297): FATAL EXCEPTION: main
03-09 22:38:24.674: E/AndroidRuntime(1297): Process: com.appquest.prodactivity, PID: 1297
03-09 22:38:24.674: E/AndroidRuntime(1297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appquest.prodactivity/com.appquest.prodactivity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.ActivityThread.access0(ActivityThread.java:144)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.os.Looper.loop(Looper.java:135)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.ActivityThread.main(ActivityThread.java:5221)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at java.lang.reflect.Method.invoke(Native Method)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at java.lang.reflect.Method.invoke(Method.java:372)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-09 22:38:24.674: E/AndroidRuntime(1297): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
03-09 22:38:24.674: E/AndroidRuntime(1297):     at com.appquest.prodactivity.MainActivity.onCreate(MainActivity.java:31)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.Activity.performCreate(Activity.java:5933)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-09 22:38:24.674: E/AndroidRuntime(1297):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

我正在使用 minimumsdk=14 和 target=21

如果您的 activity 扩展了 ActionBarActivity,则更改

actionBar = getActionBar();

actionBar = getSupportedActionBar();

**将 AndroidManifest 文件主题更改为@android:style/Theme.Holo.Light

更改 activity 的主题 像这样 android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge"

对我有用。