Marcos J.C Kichel
Marcos J.C Kichel

Reputation: 7219

getActionBar() returning null

I need my actionbar to be ready before setContentView because it is used by the navDrawerFragment but at this point:

public class BaseActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    Log.d("", getActionBar().toString());
    setContentView(R.layout.activity_base);
}

It is returning null

My theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/light_blue</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="colorAccent">@color/dark_blue</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:positiveButtonText">@color/white</item>
    <item name="android:windowActionBar">true</item>
</style>

and the declaration at manifest:

<activity
    android:name=".controller.activity.BaseActivity"
    android:label="@string/app_name" >
</activity>

Upvotes: 2

Views: 1438

Answers (3)

Human
Human

Reputation: 10815

  1. Your BaseActivity must extends from ActionBarActivity and not Activity

    public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

  2. Use getSupportActionBar(); to get the ActionBar

Upvotes: 3

mr.roboto
mr.roboto

Reputation: 31

You're using the support library (AppCompat), so you have to call getSupportActionBar()

Upvotes: 1

Tristan
Tristan

Reputation: 3578

Your problem is either one of these two things, or both. At least they should be, unless I'm insane..

Either the problem is that you are calling

getActionBar()

before you set the contentView So change it to

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_base);
    Log.d("", getActionBar().toString());
}

OR It is that you need to call

getSupportActionBar()

Try both and tell me which works!

Upvotes: 1

Related Questions