This question is asked several times in stackoverflow and I have tried all of them. But unfortunately neither is working for me.
I am trying to implement the naviga
You are inheriting from ActionBarActivity
. Hence, you need to use getSupportActionBar()
, not getActionBar()
, to get at the appcompat-v7
-supplied action bar backport.
import v7:
import android.support.v7.app.ActionBar;
then in the onCreate
method:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Use this..
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
instead of this - getActionBar().setDisplayHomeAsUpEnabled(true);
it will work perfectly.
Summay: To make sure you won't get a NullPointerException. You need to:
But in my situation an if statement is necessary to solve my App from crash. By the way, I'm using an AppCompatActivity to hold my view fragment.
public onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.list_fragment, container, false);
ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
}