Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActionBar() returns Null (AppCompat-v7 21)

My app is crashing the minute I run it after I changed my AppCompat-v7 to 21.0.0 and Compiled with no problem.

It gives me the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setIcon(int)' on a null object reference 

on this line:

getActionBar().setIcon(R.drawable.ic_action_bar); 

It works with AppCompat-v7 20.0.0, but not with 21.0.0.

like image 263
user3184899 Avatar asked Oct 18 '14 00:10

user3184899


1 Answers

You need to call getSupportActionBar() on an ActionBarActivity. Do not call getActionBar() -- that is not available on older devices, and for the new r21 edition of appcompat-v7, I would expect it to return null all the time, as the new ActionBarActivity disables and replaces the system action bar.

like image 174
CommonsWare Avatar answered Sep 17 '22 15:09

CommonsWare