Nonjoe
Nonjoe

Reputation: 121

Android ActionBar issue different API < > 21

Having issue with ActionBar NullPointerException based on different API. All my activities extend Activity .

This is my code:

Gradle

minSdkVersion 17
targetSdkVersion 23    

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'

Styles:

<style name="AppTheme" parent="Theme.AppCompat.Light">

Styles (values-v21):

<style name="AppTheme" parent="android:Theme.Material.Light">

Activity:

ActionBar actionBar = getActionBar();
assert actionBar != null;
actionBar.hide();

It works fine on API23 but give NullPointerException on lower.

Read other similar question, tried with ActionBarActivity (deprecated), AppCompatActivity, SupportActionBar but I am not able to let it works on different API.

Any suggestions? Any help would be much appreciated. Thanks in advance.

Upvotes: 0

Views: 71

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363835

You should:

  • use the AppCompatActivity
  • use getSupportActionBar() instead of getActionBar()
  • use the same theme Theme.AppCompat.Light for all devices (remove the material theme in v21)

Upvotes: 1

Terry W
Terry W

Reputation: 203

Try extending AppCompatActivity and using getSupportActionBar()

Upvotes: 0

Related Questions