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. This works fine when running on the Android 3.2 emulator, but when using Android 3.0 and 3.1 the getActionBar()
method returns null.
I find this extremely odd, and i cannot see any reason why it would do so. Is this a bug with the emulators or is there something i need to do, in order to ensure that my application has an actionbar?
SOLUTION:
I think I've found a solution for this problem.
I wasn't using the setContentView to set a layout for the activity. Instead I was using fragmentTransaction.add(android.R.id.content, mFragment, mTag)
to add a fragment to the activity.
This worked fine in 3.2, but in earlier honeycomb versions the action bar is apparently not set if you don't use the setContentView in the onCreate()
method.
So I fixed it by using the setContentView()
method in my onCreate()
method and just supplying it with a layout that contained an empty FrameLayout.
I can still use the fragmentTransaction.add(android.R.id.content, mFragment, mTag)
method the same way as before.
It's not the prettiest fix, but it works.
ActionBar needs application or activity's Theme to have an app title. Make sure you have not styled your application or activity as Theme.NOTITLE.
This answer is late but might be helpful to anyone who arrives from Google: You might well need to declare
in your
styles.xml
. It seemsfalse
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:
Use getSupportActionBar() instead of getActionBar()
To add to the other answers:
Make sure you call
setActionBar()
orsetSupportActionBar()
in youronCreate()
method before calling thegetActionBar()
:Define some Toolbar in your activity.xml, then in the onCreate():
I solve it by this changes:
android:theme="@android:style/Theme.Holo.Light" >
extends ActionBarActivity
import android.support.v7.app.ActionBarActivity
I know I am late to the party (and new to Android) on this question but I found the information here very helpful and thought I should add the findings of my own endeavours with getting ActionBar to work as I wanted in case others like me come looking for help.
I have a widget which is a floating window with no window title. I use a style theme to implement
android:windowIsFloating
,android:backgroundDimEnabled
andandroid:windowNoTitle
. The widget worked fine until I wanted to add a button that called a fragment pager with several list fragment pages and used the ActionBar. It would crash on the pager activity with a null pointer exception. I narrowed it down to the ActionBar being null. Following the findings of previous people who contributed to this thread I removed my theme from the manifest file and the ActionBar worked fine but now my window now longer floated (it was fullscreen) and it had a page title I did not want.Further research took me to the Styles and Themes API Training Guide which led me to a solution. I discovered I could add my custom theme to individual activities in the manifest file whereas before I was applying it to the application. All my windows now have the desired appearance.