I\'m having an odd problem. I am making an app with targetsdk 13.
In my main activity\'s onCreate method i call getActionBar()
to setup my actionbar. T
This may also help some people.
In my case, it was because I had not defined a context in the menu.xml
Try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.ActionBarActivity">
Instead of this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
I had the same problem and one of the solutions was to use setContentView()
before calling getActionBar()
.
But there was another thing that fixed the problem. I specified theme for the application to be @android:style/Theme.Holo.Light
.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
...
</application>
I think any theme, which has <item name="android:windowActionBar">true</item>
in it, can be used.
import android.support.v7.app.AppCompatActivity;
then
extends AppCompatActivity
then use
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In my case, I had this in my code which did not work:
@Override
protected void onCreate(Bundle savedInstanceState) {
context = getApplicationContext();
requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Then I played with the order of the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
}
And it worked!
Conclusion: requestWindowFeature should be the first thing you call in the onCreate method.
go to the AndroidManifest.xml and replace
android:theme="@style/AppTheme"
by
android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
One thing I wanted to add since I just ran into this, if you are trying to getActionBar() on an Activity that has a parent, it will return null. I am trying to refactor code where my Activity is contained inside an ActivityGroup, and it took a good few minutes for me to go "oh duh" after looking at the source of how an ActionBar gets created in source.