What to use instead of getSupportActionBar () in library 22?

There is a line in my code marked as yellow:

getSupportActionBar().setDisplayShowHomeEnabled(true);

After installing appcompat-v7: 22.1, a prompt is displayed:

"When calling a method, java.lang.nullpointerexception may occur."

What should i use instead getSupportActionBar()?

+4
source share
4 answers
getSupportActionBar().setDisplayShowHomeEnabled(true);

Must say

if (getSupportActionBar() != null)
{
   getSupportActionBar().setDisplayShowHomeEnabled(true);
}

getSupportActionBar () can return null for you to be prompted about this.

+13
source

I found another way using AppCompatDelegate:

        getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+2
source

Theme.AppCompat, setSupportActionBar(...) , getSupportActionBar() .

To bypass the warning, perform a null check or

assert getSupportActionBar() != null;

which throws an exception if the expression is not true. Both have their own capabilities.

+1
source

Use NoActionBarin styles? Confirm yours style.xmlor topic,if("NoActionBar") => nullpointer =D

+1
source

Source: https://habr.com/ru/post/1584924/


All Articles