Search code examples
androidgradleandroid-actionbaradt

ActionBar is null after migration to Gradle


This is a really weird error as far as I can understand. I just migrated a project from ADT to Gradle. And the same exact code is now crashing on startup. It seems the problem is that the ActionBar that previously was perfectly valid now is null.

The code is within a class that extends Activity and is called from within onCreate

    setContentView(R.layout.activity_main);
    ActionBar actionBar = getActionBar();
    if (actionBar==null) Log.d(TAG,"AB null.");

Not sure what more code to post, since I'm really puzzeled why this worked just 30 minutes ago and not at all now. The project is not using any support package, and only targets 4.0 and later.


Solution

  • Theme.Light does not have an action bar. That is the old Android 1.x/2.x theme, with the old title bar (thin grey strip with the app's name).

    Theme.Holo.Light, and a targetSdkVersion of 11+, will give you an action bar.