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

前端 未结 14 2208
醉酒成梦
醉酒成梦 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:32

    Try this :

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

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

提交回复
热议问题