getActionBar() 返回 null [英] getActionBar() returns null

查看:32
本文介绍了getActionBar() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题.我正在使用targetsdk 13制作一个应用程序.

I'm having an odd problem. I am making an app with targetsdk 13.

在我的主要活动的 onCreate 方法中,我调用 getActionBar() 来设置我的操作栏.这在 Android 3.2 模拟器上运行时工作正常,但在使用 Android 3.0 和 3.1 时,getActionBar() 方法返回 null.

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?

解决方案:我想我已经找到了解决这个问题的方法.我没有使用 setContentView 为活动设置布局.相反,我使用 fragmentTransaction.add(android.R.id.content, mFragment, mTag) 向活动添加片段.这在 3.2 中运行良好,但在早期的蜂窝版本中,如果您不在 onCreate() 方法中使用 setContentView,则显然不会设置操作栏.所以我通过在我的 onCreate() 方法中使用 setContentView() 方法来修复它,并只为它提供一个包含空 FrameLayout 的布局.我仍然可以像以前一样使用 fragmentTransaction.add(android.R.id.content, mFragment, mTag) 方法.

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.

推荐答案

可以使用 getSupportActionBar() 代替 getActionBar() 方法.

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

这篇关于getActionBar() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆