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

I had the same problem and one of the solutions was to use setContentView() before calling getActionBar().

But there was another thing that fixed the problem. I specified theme for the application to be @android:style/Theme.Holo.Light.

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light" >
    ...
</application>

I think any theme, which has <item name="android:windowActionBar">true</item> in it, can be used.

查看更多
余生请多指教
3楼-- · 2018-12-31 15:27

In my case, I had this in my code which did not work:

@Override
protected void onCreate(Bundle savedInstanceState) {
    context = getApplicationContext();

    requestWindowFeature(Window.FEATURE_ACTION_BAR);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
}

Then I played with the order of the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_ACTION_BAR);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    context = getApplicationContext();
}

And it worked!

Conclusion: requestWindowFeature should be the first thing you call in the onCreate method.

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

One thing I wanted to add since I just ran into this, if you are trying to getActionBar() on an Activity that has a parent, it will return null. I am trying to refactor code where my Activity is contained inside an ActivityGroup, and it took a good few minutes for me to go "oh duh" after looking at the source of how an ActionBar gets created in source.

查看更多
怪性笑人.
5楼-- · 2018-12-31 15:29
import android.support.v7.app.AppCompatActivity;

then

extends AppCompatActivity

then use

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
查看更多
查无此人
6楼-- · 2018-12-31 15:30

Try extending your Activity class from ActionBarActivity. This solved it for me. Do something like the following:

public class MyActivity extends ActionBarActivity
{
  . . .

In my case the class was extending only from Activity.

查看更多
几人难应
7楼-- · 2018-12-31 15:31

I ran into this problem . I was checking for version number and enabling the action bar only if it is greater or equal to Honeycomb , but it was returning null. I found the reason and root cause was that I had disabled the Holo Theme style in style.xml under values-v11 folder.

查看更多
登录 后发表回答