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

前端 未结 14 2270
醉酒成梦
醉酒成梦 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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();
        }
    

提交回复
热议问题