I tried to set a title for an ActionBar
with the following code:
@Override
public void onResume() {
((MainActivity) getActivity()).getSupportActionBar().setTitle(getResources().getString(R.string.artist));
super.onResume();
}
but Android Studio shows me this warning:
I searched on StackOverflow that it will be fixed by adding this code if(getSupportActionBar()!=null)
in front of my code. But it causes an error in my script.
I'm not sure how to fix this.
You have several options to do what you asked for:
No need to explain further, this is not an error
Wrap your line with 'if' statement to make sure it's not null
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle(getString(R.string.artist));
}
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(getString(R.string.artist));
If you use 'getSupportActionBar' in multiple locations, you can remove all of those warnings and in return you will only receive a warning on the @NonNull usage.
@NonNull
@Override
public ActionBar getSupportActionBar() {
return super.getSupportActionBar();
}