Android ActionBar issue different API < > 21

73 Views Asked by At

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.

2

There are 2 best solutions below

0
Gabriele Mariotti On BEST ANSWER

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)
0
Terry W On

Try extending AppCompatActivity and using getSupportActionBar()