How to fix getActionBar method may cause java.lang.NullPointerException

I use the toolbar as my action in action. I am trying to add the getActionBar().setDisplayHomeAsUpEnabled(true); method getActionBar().setDisplayHomeAsUpEnabled(true); to the Activity.java file to navigate up for older devices.

In Android Studio, the following error message appears:

A method call may throw java.lang.NullPointerException

Navigating up on the toolbar works great on new devices ... now I'm trying to figure out how to make sure that it works for older devices. Please inform.

From build.gradle:

 dependencies { compile "com.android.support:appcompat-v7:22.1.0" } 

From AndroidManifest.xml:

 android:theme="@style/Theme.AppCompat.NoActionBar.FullScreen" 

From styles.xml

 <style name="Theme.AppCompat.NoActionBar.FullScreen" parent="AppTheme"> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="android:windowFullscreen">true</item> 

from Activity.java

 public class CardViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cardviewinput); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if (toolbar != null) { // Up navigation to the parent activity for 4.0 and earlier getActionBar().setDisplayHomeAsUpEnabled(true); toolbar.setNavigationIcon(R.drawable.ic_action_previous_item); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } } 
+72
android android-actionbar toolbar
Apr 22 '15 at 1:14
source share
14 answers

In fact, Android Studio does not show you an error message, this is just a warning.

Some answers suggest using assertion, the Dalvik assertion runtime is disabled by default, so you need to actually enable it so that it really does something. In this case (the statement is disabled), what you essentially do is simply fooling Android Studio so as not to show you a warning. In addition, I prefer not to use "assert" in production code.

In my opinion, you should do it very simply.

 if(getActionBar() != null){ getActionBar().setDisplayHomeAsUpEnabled(true); } 

Update: If you are using the action bar support library version, you must replace getActionBar () with getSupportActionBar ().

 if(getSupportActionBar() != null){ getSupportActionBar().setDisplayHomeAsUpEnabled(true); } 
+123
Mar 01 '16 at 9:50
source share

You must first install the toolbar as an ActionBar support. Then, if you are sure that he will be there all the time, just approve him as! = Null. This will tell the compiler that it will not be NULL, so a null check is skipped.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cardviewinput); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); assert getSupportActionBar() != null; getSupportActionBar().setDisplayHomeAsUpEnabled(true); // it getSupportActionBar() if you're using AppCompatActivity, not getActionBar() } 
+34
Apr 22 '15 at 10:40
source share

Thanks to Andrew for your answer. If you have Nav Drawer or something else using getSupportActionBar (), you need to add assert getSupportActionBar ()! = Null;

Peace,

Example:

 @Override public void setTitle(CharSequence title) { mTitle = title; assert getSupportActionBar() != null; getSupportActionBar().setTitle(mTitle); } 
+8
Apr 25 '15 at 5:53
source share

Try the following:

 private ActionBar getActionBar() { return ((AppCompatActivity) getActivity()).getSupportActionBar(); } 
+4
Jul 10 '15 at 7:07
source share

What I did overrides the getSupportActionBar() method in my base operation and adds the @NonNull annotation. That way, I get only one lint warning in the base action of how I use the @NonNull annotation for something that has the @Nullable annotation.

  @NonNull @Override public ActionBar getSupportActionBar() { // Small hack here so that Lint does not warn me in every single activity about null // action bar return super.getSupportActionBar(); } 
+3
Mar 30 '16 at 8:51
source share

I created a generic class, for example:

 public final class Cast { private Cast() {} /** * Helps to eliminate annoying NullPointerException lint warning. */ @android.support.annotation.NonNull public static <T> T neverNull(T value) { return value; } } 

then I can use it for any call with a NullPointerException warning, for which I am sure that this will never happen, for example.

 final ActionBar actionBar = Cast.neverNull(getSupportActionBar()); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); 

PS don't forget to add "com.android.support:support-annotations" to your gradle file.

+3
Jun 09 '16 at 7:31
source share

add assert getSupportActionBar() != null; to getSupportActionBar().setDisplayHomeAsUpEnabled(true);

+2
Oct 17 '15 at 6:52
source share
  if(actionBar != null) { actionBar.setHomeButtonEnabled(true); actionBar.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.action_bar_gradient)); } 
0
Jun 15 '16 at 6:27
source share

use this theme: android:theme="@style/Theme.AppCompat.Light.NoActionBar"

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("Title"); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setHomeAsUpIndicator(R.drawable.ic_action_previous_item); actionBar.setDisplayHomeAsUpEnabled(true); 
0
Jun 15 '16 at 6:46
source share

Alternatively, you can assert an actionbar so as not to be null. Add a statement before calling the actionbar as follows

 assert getSupportActionBar() != null; 

The final snippet will look like this:

  setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); assert getSupportActionBar() != null; getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
0
Apr 03 '18 at 19:32
source share

Try this:

 setSupportActionBar (toolbar); if(getSupportActionBar () != null) { assert getSupportActionBar () != null; getSupportActionBar ().setDisplayHomeUpEnabled(true); } 

Note that setSupportActionBar (toolbar) must be before getSupportActionBar ().

0
Jan 24 '19 at 0:25
source share
  if(getSupportActionBar() != null){ getSupportActionBar().setDisplayHomeAsUpEnabled(true); } OR 

Replace MainActivity extends AppCompatActivity with public class MainActivity extends AppCompatActivity

0
Feb 01 '19 at 11:22
source share

just check getSupportActionBar is not nil

  setSupportActionBar(toolbar); if(getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("Daily Shopping List"); } 
0
Apr 15 '19 at 8:17
source share

If you import

 android.app.ActionBar 

You must use getActionBar ()

and if you import

 android.support.v7.app.ActionBar 

use getSupportActionBar ()

0
Apr 15 '19 at 8:58
source share



All Articles