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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章