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:33

Can use getSupportActionBar() instead of getActionBar() method.

查看更多
唯独是你
3楼-- · 2018-12-31 15:33

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">
查看更多
临风纵饮
4楼-- · 2018-12-31 15:37

The main reason for that is using themes that are not supporting ActionBar:

In manifest file add the following either in your target activity or application element (if you want to unify the theme over whole application)

Examples of themes that are supporting action bar "Theme.AppCompat.Light" or "Theme.Holo.Light" ...

android:theme="@android:style/Theme.Holo.Light"

It is better to put all styles in styles.xml and use it everywhere using "@style/themName" so the previous one will be

android:theme="@style/AppTheme"

and styles.xml will have the following:

 <style name="AppTheme" parent="Theme.AppCompat.Light">

Hints:

  • There is some themes that can not be used in old SDKs like "@android:style/Theme.Holo.Light.DarkActionBar" is not supported before SDKs version 14.
  • To allow your app to support minimum specific version of SDK you could add the following under <app> element:

    <uses-sdk android:minSdkVersion="14" />
    
  • To specify min SDK version in AndroidStudio, you could by using app's Gradle file.

    android{
      defaultConfig{
        minSdkVersion 14
        targetSdkVersion 21
      }
    }
    
查看更多
查无此人
5楼-- · 2018-12-31 15:39

I faced the above issue where getActionBar() method returns null. I was calling the getActionBar() after setting the setContentView() and still its returning a null.

I resolved the issue by setting the min-sdk version in Android Manifest file that was missing initially. <uses-sdk android:minSdkVersion="11" />

查看更多
孤独寂梦人
6楼-- · 2018-12-31 15:40

You have to define window type as actionbar before activity render its view.

use

requestWindowFeature(Window.FEATURE_ACTION_BAR);

before calling setContentView() method.

查看更多
弹指情弦暗扣
7楼-- · 2018-12-31 15:41

android.support.v7.app.ActionBar actionBar = getSupportActionBar();

works pretty quickly

查看更多
登录 后发表回答