I'm trying to add the action bar programatically as shown in the dev documentaion but I'm coming across an error. My minSdk is set to 11, my application has one layout and one activity and the only code in the activity is:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
ActionBar actionBar = getActionBar();
actionBar.show();
}
If I take out those last two lines then my app runs. I know that the holo theme include the actionbar automatically, but I don't like the editText
views. Any ideas on why this is happening. Should I be using the HoloTheme
and themeing the views differently?
Again, I'm not getting any errors in eclipse. My program is crashing from what I can decipher from logcat as an null pointer exception.
What Darshan Said ... kind of
To go a little further I would check out ActionBarSherlock, in combination with the support library you can then add the action bar to pretty much any version of android that is 2.x+
To get a full screen / no title bar you would create your own theme from one of the ABS theme's similar to
Applying that theme in your onCreate and then getting the action bar to show is the same (except you would use the ABS support prefix ) so basically
I am assuming you can do the same for a standard action bar (using the holo theme as a base) since ABS is pretty much a duplicate functionality wise (and much of the source, ABS is mostly a wrapper class) it even detects and then uses the native implementation on 14 and above (11 and in-between has some missing features / fixes )
You can take a look at ABS here and it looks like he just updated it to JB
-- In your resources folder under values create a themes.xml file ( /res/values/themes.xml )
Then put this in there and give the above a try, but of course in setTheme( replace the name with Theme_My_Holo_FullScreen and dont use the getSupportActionbar(). prefix, I never bother using the native implementation but it should work the same.
I ran into the same problem. I eventually came up with a reasonable solution. I have a min api version 8 with a target api of 11. I don't like the holo styles either so I override the styles like the following:
First, you have to create a "res/values-v11" folder. You probably already have a "values" folder, just create a "values-v11" at the same level (doesn't matter what your target API level is...aways use "values-v11").
Once that is created, you create an XML document containing your theme in each of the folders. The original values folder will be used for anything less than android 3.0 (< api 11). Your theme would look something like this.
Notice that I am NOT using the Holo theme but instead using the Theme.Black.NoTitleBar theme as the parent. From there, I set the options to hide the window title and action bar for things lower than api 11.
Then, I create a second theme document in the "values-v11" folder and set it up like this:
In the above theme, it enables the action bar and sets it up with the Holo theme, but leaves all the other elements alone. So the action bar looks "holo"-ish but everything else stays exactly as it appears in the lower versions (and I don't get the ugly text entry that comes with Holo).
Then in the manifest if I want to have the actionbar show up, I just set the theme for that activity to
android:theme="@style/Theme.Black.OptionalActionBar"
I did this specifically because I failed on my layouts by relying on the, now deprecated, hardware menu button. With the use of the themes above, I can setup the action bar if it is required or leave it as menus if it is an older device. Then, all I have to do is set a single property on my menu creation methods to support 2.0 through 4.0 seamlessly.
At any rate, take a look and see if this gets you to where you need to be.
In my mind it is a lot easier than the other approaches because you don't have to remember to request the actionbar or do anything in code. You just set your theme on your activity...and off you go.
From the documentation you linked to:
Since you're using a theme without an action bar,
getACtionBar()
is returningnull
, and then you're attempting to callshow()
on thatnull
, resulting in an Exception being thrown.So that explains the error you're getting. As far as what to do about it, the documentation for ActionBar says:
That gives you two easy options to have an ActionBar without using a Holo theme. This is probably simplest in your case:
Update: You should also switch to just
Theme.Black
without theNoTitleBar
part, as that prevents the ActionBar from working.