getActionBar()返回null - getActionBar() returns null

问题:

I'm having an odd problem. 我有一个奇怪的问题。 I am making an app with targetsdk 13. 我正在使用targetsdk 13制作应用程序。

In my main activity's onCreate method i call getActionBar() to setup my actionbar. 在我的主要活动的onCreate方法中,我调用getActionBar()来设置我的操作栏。 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. 这在Android 3.2模拟器上运行时工作正常,但在使用Android 3.0和3.1时, getActionBar()方法返回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. 我没有使用setContentView来设置活动的布局。 Instead I was using fragmentTransaction.add(android.R.id.content, mFragment, mTag) to add a fragment to the activity. 相反,我使用fragmentTransaction.add(android.R.id.content, mFragment, mTag)向活动添加片段。 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. 这在3.2中运行良好,但在早期的蜂窝版本中,如果你不在onCreate()方法中使用setContentView,那么动作栏显然没有设置。 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. 所以我通过在onCreate()方法中使用setContentView()方法修复它,并为其提供包含空FrameLayout的布局。 I can still use the fragmentTransaction.add(android.R.id.content, mFragment, mTag) method the same way as before. 我仍然可以像以前一样使用fragmentTransaction.add(android.R.id.content, mFragment, mTag)方法。

It's not the prettiest fix, but it works. 这不是最漂亮的修复,但它确实有效。


解决方案:

参考一: https://en.stackoom.com/question/SoRI
参考二: https://stackoom.com/question/SoRI
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章