Ardi
Ardi

Reputation: 1871

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

There is a line in my code, that marked as yellow:

getSupportActionBar().setDisplayShowHomeEnabled(true);

After installing appcompat-v7:22.1 it shows a hint:

"Method invocation may produce java.lang.nullpointerexception".

What should be used instead of getSupportActionBar()?

Upvotes: 4

Views: 6480

Answers (4)

Rurouni_X
Rurouni_X

Reputation: 13

Are using NoActionBar in styles? Verify your style.xml or you theme, if("NoActionBar") => nullpointer =D

Upvotes: 1

Ardi
Ardi

Reputation: 1871

I found another way, using AppCompatDelegate:

        getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Upvotes: 2

Eugen Pechanec
Eugen Pechanec

Reputation: 38243

If you're extending a Theme.AppCompat which has an action bar or have called setSupportActionBar(...) yourself, calling getSupportActionBar() is safe.

To get around the warning do a null check or

assert getSupportActionBar() != null;

which will throw an exception if the expression is not true. Both have their uses.

Upvotes: 1

Anthony
Anthony

Reputation: 3074

getSupportActionBar().setDisplayShowHomeEnabled(true);

Should say

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

getSupportActionBar() can return null so you the hint is telling you about this.

Upvotes: 13

Related Questions