Getactionbar().setdisplayhomeasupenabled(true) Throws Nullpointerexception
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 navigation between tw
Solution 1:
You are inheriting from ActionBarActivity
. Hence, you need to use getSupportActionBar()
, not getActionBar()
, to get at the appcompat-v7
-supplied action bar backport.
Solution 2:
import v7:
import android.support.v7.app.ActionBar;
then in the onCreate
method:
ActionBaractionBar= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Solution 3:
Use this..
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
instead of this - getActionBar().setDisplayHomeAsUpEnabled(true);
it will work perfectly.
Solution 4:
Summay: To make sure you won't get a NullPointerException. You need to:
- Import the right library target to you minium SDK version.
- Use the right themes, just as the question owner said.
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.
publiconCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState){
Viewview= inflater.inflate(R.layout.list_fragment, container, false);
ActionBaractionBar= getActivity().getActionBar();
if (actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
Post a Comment for "Getactionbar().setdisplayhomeasupenabled(true) Throws Nullpointerexception"