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

查看:193
本文介绍了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.

这不是prettiest解决,但它的工作原理。

It's not the prettiest fix, but it works.

推荐答案

我发现这只是等式的一半。您还需要你的活动的窗口,有肉眼可见的称号。我写关于这个话题

I found this is just a half of the equation. You also need your Activity's window to have the title visible. I wrote an entire blog post about this topic.

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

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