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

前端 未结 14 2088
醉酒成梦
醉酒成梦 2020-12-02 18:06

I am using a toolbar as my actionbar in an activity. I am trying to add the method getActionBar().setDisplayHomeAsUpEnabled(true); to the Activity.java file fo

相关标签:
14条回答
  • 2020-12-02 18:37

    What I have done is override the getSupportActionBar() method in my base Activity and add a @NonNull annotation. This way, I only get one lint warning in the base activity about how I use @NonNull annotation for something that has a @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();
        }
    
    0 讨论(0)
  • 2020-12-02 18:40

    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 讨论(0)
  • 2020-12-02 18:41

    Thank You Andrew for your answer. If you have a Nav Drawer or something else that uses getSupportActionBar() you need to add assert getSupportActionBar() != null;

    Peace,

    Example:

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle(mTitle);
    }
    
    0 讨论(0)
  • 2020-12-02 18:46

    If you are importing

    android.app.ActionBar 
    

    you have to use getActionBar()

    and if you are importing

    android.support.v7.app.ActionBar
    

    use getSupportActionBar()

    0 讨论(0)
  • 2020-12-02 18:48

    Actually Android Studio isn't showing you an "error message", it's just a warning.

    Some answers propose the use of an assertion, Dalvik runtime has assertion turned off by default, so you have to actually turn it on for it to actually do something. In this case (assertion is turned off), what you're essentially doing is just tricking Android Studio to not show you the warning. Also, I prefer not to use "assert" in production code.

    In my opinion, what you should do is very simple.

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

    Update: In case you're using the support library version of the Action Bar, you should replace getActionBar() with getSupportActionBar().

    if(getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    
    0 讨论(0)
  • 2020-12-02 18:51

    First off, you need to set the toolbar as the support ActionBar. Then if you're certain it's going to be there all the time, just assert it as != null. This will tell the compiler it won't be null, so the null check passes.

    @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's getSupportActionBar() if you're using AppCompatActivity, not getActionBar()
    }
    
    0 讨论(0)
提交回复
热议问题