getActionBar() returns null

2018-12-31 14:46发布

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.

23条回答
残风、尘缘若梦
2楼-- · 2018-12-31 15:22

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.

<application
    android:name="com.xxx.yyy"
    android:debuggable="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code


<activity
        android:name="com.xxx.yyy.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@style/Theme.NoTitle"  // remove this line if you have in your code
        android:windowSoftInputMode="adjustResize|stateHidden" > 
查看更多
忆尘夕之涩
3楼-- · 2018-12-31 15:22

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.

查看更多
若你有天会懂
4楼-- · 2018-12-31 15:23

Use getSupportActionBar() instead of getActionBar()

查看更多
看淡一切
5楼-- · 2018-12-31 15:23

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);
查看更多
初与友歌
6楼-- · 2018-12-31 15:25

I solve it by this changes:

  1. change in minifest android:theme="@android:style/Theme.Holo.Light" >
  2. add to class extends ActionBarActivity
  3. add import to class import android.support.v7.app.ActionBarActivity
查看更多
宁负流年不负卿
7楼-- · 2018-12-31 15:26

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 and android: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.

查看更多
登录 后发表回答