1

I'm just getting started with Android development and I'm following the tutorials by Google. I have a problem with action bars, where the getActionBar method returns null, although as per the tutorial it shouldn't. I reviewed similar StackOverflow questions, but none of the proposed solutions works.

Here is my onCreate code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>

</manifest>

Any ideas?

EDIT: adding styles.xml (it's the default one, created by Eclipse)

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>
M.S
  • 1,580
  • 1
  • 13
  • 22

1 Answers1

7

For AppCompat v21:

All of your Activities must extend from ActionBarActivity

You then use getSupportActionBar() to retrieve the Action Bar.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • My activities already extend ActionBarActivity – M.S Oct 23 '14 at 22:00
  • So use `getSupportActionBar()` instead of `getActionBar()`. – ianhanniballake Oct 23 '14 at 22:00
  • That worked! Can you explain why I need to use getSupportActionBar instead of getActionBar, even though the tutorial stipulates that getActionBar should be used when min SDK is 11 or higher? – M.S Oct 23 '14 at 22:18
  • Unfortunately some of the documentation has not been updated since the release of the Android 5.0 (API 21) SDK and AppCompat v21 release. As there are considerable changes between the Material Design action bar of 5.0 and previous versions, AppCompat now always creates its own action bar which can only be accessed via `getSupportActionBar()`. This allows you to use many of the cool new features of the Action Bar added in 5.0 on lower API levels. – ianhanniballake Oct 23 '14 at 22:21