I'm having an odd problem. I am making an app with targetsdk 13.

In my main activity's onCreate method i call getActionBar() to setup my actionbar. This works fine when running on the Android 3.2 emulator, but when using Android 3.0 and 3.1 the getActionBar() method returns null.

I find this extremely odd, and i cannot see any reason why it would do so. Is this a bug with the emulators or is there something i need to do, in order to ensure that my application has an actionbar?

SOLUTION: I think I've found a solution for this problem. I wasn't using the setContentView to set a layout for the activity. Instead I was using fragmentTransaction.add(android.R.id.content, mFragment, mTag) to add a fragment to the activity. This worked fine in 3.2, but in earlier honeycomb versions the action bar is apparently not set if you don't use the setContentView in the onCreate() method. So I fixed it by using the setContentView() method in my onCreate() method and just supplying it with a layout that contained an empty FrameLayout. I can still use the fragmentTransaction.add(android.R.id.content, mFragment, mTag) method the same way as before.

It's not the prettiest fix, but it works.


Can use getSupportActionBar() instead of getActionBar() method.


If you are using the support library

import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

use getSupportActionBar() instead of getActionBar()

* Update:

The class ActionBarActivity now is deprecated:

import android.support.v7.app.ActionBarActivity;

I recommend to use:

import android.support.v7.app.AppCompatActivity

  1. if you are using android.support.v7.app.AppCompatActivity

    public class HomeActivity extends AppCompatActivity {

Then you should be using android.support.v7.app.ActionBar

  ActionBar ab = getSupportActionBar();
  1. If you are using android.support.v4.app.FragmentActivity

    public class HomeActivity extends FragmentActivity {

then you should be using android.app.ActionBar

    ActionBar ab = getActionBar();
  1. If you are using android.support.v7.app.ActionBarActivity

    public class HomeActivity extends ActionBarActivity {

you should be using android.support.v7.app.ActionBar

   ActionBar ab = getSupportActionBar();

You have to define window type as actionbar before activity render its view.

use

requestWindowFeature(Window.FEATURE_ACTION_BAR);

before calling setContentView() method.


I faced the above issue where getActionBar() method returns null. I was calling the getActionBar() after setting the setContentView() and still its returning a null.

I resolved the issue by setting the min-sdk version in Android Manifest file that was missing initially. <uses-sdk android:minSdkVersion="11" />