Android - getActionBar() returns null after upgrading to SDK 21

I was working on a new app in Android Studio Beta with target SDK 20. It was all well and good, but after updating target SDK version to 21 (and appcompat-v7 to 21.0.0) the app started crashing on launch.

Quick debugging revealed that the issue was cased by the line of code actionBar = getActionBar(); inside main activity onCreate method. getActionBar() was now returning NULL.

After some digging I resolved the issue by changing application theme from Theme.AppCompat to android:Theme.Holo. This will only work if your minimum SDK is 14 or higher, which was the case in this instance.

One additional note - after doing this, my app started crashing when launching certain activities. The error was:

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Activities in question were extending ActionbarActivity. As the error above suggests such activities must use AppCompat themes. Since ActionbarActivity is only required for backwards compatibility reasons with API levels below 11, I fixed this by changing these activities to extend Activity. instead of ActionbarActivity.

 

Newsletter

Subscribe to receive occasional updates on new posts.
Your email will not be used for any other purpose and you can unsubscribe at any time.
Please wait