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

前端 未结 4 578
逝去的感伤
逝去的感伤 2021-01-20 01:23

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

getSupportActionBar().setDisplayShowHomeEnabled(true);

After installing appcompat-v7

相关标签:
4条回答
  • 2021-01-20 01:56

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

    0 讨论(0)
  • 2021-01-20 02:12

    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.

    0 讨论(0)
  • 2021-01-20 02:13

    I found another way, using AppCompatDelegate:

            getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
  • 2021-01-20 02:18
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    

    Should say

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

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

    0 讨论(0)
提交回复
热议问题