25

Calling getActionBar returns null. This has been frequently reported so I've made sure to include the solutions others have used: My minSdkVersion=11, I do have a titlebar, and I'm calling getActionBar after setContentView. Also, my activity is not a child activity.

setContentView(R.layout.main);

// experiment with the ActionBar 
ActionBar actionBar = getActionBar();
actionBar.hide();

Device is a Samsung Galaxy Tab 10.1 running Android 3.2

Thanks in advance for any ideas or suggestions!

Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
user316117
  • 7,971
  • 20
  • 83
  • 158

14 Answers14

58

It seems you need to request having an Actionbar (!= titlebar) either via a Theme or via below code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // The Action Bar is a window feature. The feature must be requested
    // before setting a content view. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.main);

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}

Code from [here]

zapl
  • 63,179
  • 10
  • 123
  • 154
11

It seems there are several conditions which need to be met in order for this to work. The one which stumped me for a long time was this:

Make sure your activity extends Activity (NOT ActionBarActivity).

public class MagicActivity extends Activity

Make sure your app manifest has something like

<application
    ...
    android:theme="@android:style/Theme.Holo" >

and make sure your activity layout has

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    android:theme="@android:style/Theme.WithActionBar" >

-Mark

Mark Smith
  • 880
  • 1
  • 8
  • 25
10

I seemed to solve all my errors by switching getActionBar() with getSupportActionBar(). I had tried different themes, adding getWindow().requestFeature(Window.FEATURE_ACTION_BAR);, made sure the setContentView(view) was first and all the other things seen before.

ContinuousError
  • 125
  • 2
  • 9
7

Action bar needs theme or activity with app title to be there. Make sure you have not styled your application or activity as Theme.NOTITLE.

<application
    android:name="com.xxx.yyy.Application"
    android:debuggable="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code


<activity
        android:name="com.xxx.yyy.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@style/Theme.NoTitle"  // remove this line if you have this in your code
        android:windowSoftInputMode="adjustResize|stateHidden" > 
Himanshu Virmani
  • 2,450
  • 1
  • 24
  • 34
4

I was trying to get the ActionBar from within a fragment inside an ActionBarActivity using the support library. I had to do this:

ActionBarActivity actionBarActivity = (ActionBarActivity)getActivity();
ActionBar actionBar = actionBarActivity.getSupportActionBar();

My next quest is to figure out how to make this generic. For example, this code feels dirty because it requires my Fragment to know both that it's being used by an ActionBarActivity and using the support library. I think it should be a method that checks getActivity().getActionBar() first followed by the code above catching the ClassCastException if the parent Activity is not an ActionBarActivity.

Harvey
  • 5,703
  • 1
  • 32
  • 41
  • Only this answer works for me. Even thought I was extending ActionBarActivity, getActionBar() is returning null :( – Vivek Jan 29 '15 at 11:02
2

If you create an application project for API14 or above, there will be a values-14 folder. Make sure that folder exists and it has a styles.xml file with:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
</style>

I got this error because I created a new project and I didn't want any extra values folders in my project, so I just kept the values folder and removed the values-v11 and values-v14 folders. But apparently the default theme for API14 and above requires an ActionBar in it's parent XML tag if you want to use the ActionBar. So I assumed I could do that incorrectly! Hope this helps somebody. Your most likely to resolve your issues with the above answers though.

Lou Morda
  • 5,078
  • 2
  • 44
  • 49
2

This fixed my issue:

android.support.v7.app.ActionBar ab = getSupportActionBar();
Fidel90
  • 1,828
  • 6
  • 27
  • 63
Jayakrishnan
  • 4,457
  • 29
  • 29
1

The real answer should be combination of both @zapl and @Himanshu Virmani

For Android Manifest:

<application
    android:name="com.xxx.yyy.Application"
    android:debuggable="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code


<activity
        android:name="com.xxx.yyy.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@style/Theme.NoTitle"  // remove this line if you have this in your code
        android:windowSoftInputMode="adjustResize|stateHidden" > 

For Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // The Action Bar is a window feature. The feature must be requested
    // before setting a content view. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);
    // Hide the status bar for Android 4.1 and higher
    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}
1

Make sure you extend ACTIONBARACTIVITY and not ACTIVITY This should solve your problem

Sagar Devanga
  • 2,815
  • 27
  • 30
1

just change two thing 1) manifest file edit android:theme="@android:style/Theme.Holo" >

2) Fragment file add android:theme="@android:style/Theme.WithActionBar"

Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20
1

Change the order of system request:

@Override
protected void onCreate(Bundle savedInstanceState) {

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    super.onCreate(savedInstanceState);    
    setContentView(R.layout.main);

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}
Srikrishnan Suresh
  • 729
  • 2
  • 13
  • 31
0

In case when you are developing an app which is targeting sdk>=14 , and you are extending AppCompat to support Material theme in previous versions also . When you want to hide the items from your action bar like the back button you need to do this : In your activity (not in the fragment class) If you will you use getActionBar() it will return null. as in Appsupport theme getSupportAcionBar() will return the action bar.

Here is the link if you want to follow from DevByte

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_detail);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);




}
Antroid
  • 391
  • 4
  • 15
0

maybe your activity's style is based NoActionBar

perry
  • 856
  • 1
  • 10
  • 22
0

Step 01:

change your class parent. Activity to AppCompatActivity

i.e:

public class BluetoothActivity extends AppCompatActivity {
//Your Code
}

Step 02:

Create a Global Variable as follows. (I am using androidx. So this is it import looks like)

androidx.appcompat.app.ActionBar actionBar;

Step 03:

Use dark Action bar as Follows.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your style -->
</style>

Step 04:

Assign getSupportActionBar(); to the variable, you created as a global in the onCreate method.

setContentView(R.layout.activity_bluetooth);
actionBar = getSupportActionBar(); //Create this After setContentView
actionBar.show(); //this is for Experimental process only

Step 05:

Now you can do whatever you want to do with your actionBar

i.e

actionBar.setTitle("Profile");
gsm
  • 2,348
  • 17
  • 16