Search code examples
androidtoolbarappcompatactivity

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()?


Solution

  • getSupportActionBar().setDisplayShowHomeEnabled(true);
    

    Should say

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

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