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

go to the AndroidManifest.xml and replace

android:theme="@style/AppTheme"

by 

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

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

I had the same issue. It solved by chaning App theme in styles.xml

Before

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

After

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
查看更多
只靠听说
4楼-- · 2018-12-31 15:46

If you are using the support library

import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

use getSupportActionBar() instead of getActionBar()

* Update:

The class ActionBarActivity now is deprecated:

import android.support.v7.app.ActionBarActivity;

I recommend to use:

import android.support.v7.app.AppCompatActivity
查看更多
大哥的爱人
5楼-- · 2018-12-31 15:46
  1. 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();
  1. 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();
  1. 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();
查看更多
临风纵饮
6楼-- · 2018-12-31 15:49

Just check the implementation of source code by command click:

    private void initWindowDecorActionBar() {
    Window window = getWindow();

    // Initializing the window decor can change window feature flags.
    // Make sure that we have the correct set before performing the test below.
    window.getDecorView();

    if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
        return;
    }

    mActionBar = new WindowDecorActionBar(this);
    mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);

    mWindow.setDefaultIcon(mActivityInfo.getIconResource());
    mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
}

requestWindowFeature(Window.FEATURE_ACTION_BAR); Fixed my issue as I saw requestWindowFeature(Window.FEATURE_ACTION_BAR) is failing; code is open source use it !!

查看更多
登录 后发表回答