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

此生再无相见时 提交于 2019-12-01 19:36:56
getSupportActionBar().setDisplayShowHomeEnabled(true);

Should say

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

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

I found another way, using AppCompatDelegate:

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

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.

Rurouni_X

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!