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
Use getSupportActionBar() instead of getActionBar()
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
getSupportActionBar()
instead of getActionBar()
import android.support.v7.app.ActionBarActivity;
I recommend to use:
import android.support.v7.app.AppCompatActivity
if you are using android.support.v7.app.AppCompatActivity
public class HomeActivity extends AppCompatActivity {
Then you should be using android.support.v7.app.ActionBar
ActionBar ab = getSupportActionBar();
If you are using android.support.v4.app.FragmentActivity
public class HomeActivity extends FragmentActivity {
then you should be using android.app.ActionBar
ActionBar ab = getActionBar();
If you are using android.support.v7.app.ActionBarActivity
public class HomeActivity extends ActionBarActivity {
you should be using android.support.v7.app.ActionBar
ActionBar ab = getSupportActionBar();
This answer is late but might be helpful to anyone who arrives from Google: You might well need to declare
<item name="android:windowActionBar">true</item>
in your styles.xml
. It seems false
can be the default. You also need to be on API 11 or higher.
More details can be found in the documentation here. Specifically, quote:
Tip: If you have a custom activity theme in which you'd like to remove the action bar, set the android:windowActionBar style property to false. However, if you remove the action bar using a theme, then the window will not allow the action bar at all, so you cannot add it later—calling getActionBar() will return null.
I solve it by this changes:
android:theme="@android:style/Theme.Holo.Light" >
extends ActionBarActivity
import android.support.v7.app.ActionBarActivity
To add to the other answers:
Make sure you call setActionBar()
or setSupportActionBar()
in your onCreate()
method before calling the getActionBar()
:
Define some Toolbar in your activity.xml, then in the onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
// Now you can use the get methods:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);