2

我覺得我必須失去一些瘋狂的東西。我正在將我編寫的代碼轉換爲使用ABS(並且確實有效),並將其切換爲使用本機ActionBar。我將min SDK設置爲14,並將其切換爲框架版本,現在我無法使ActionBar存在。getActionBar()== null當它不應該

Activity

public class HomeActivity extends Activity { 

    @Override 
    protected void onCreate(@CheckForNull Bundle icicle) { 
     super.onCreate(icicle); 
     getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
    } 
} 

我AndroidManifest:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17"/> 
... 
<activity android:launchMode="singleTop" android:name=".ui.HomeActivity" android:theme="@android:style/Theme.Holo"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN"/> 
     <category android:name="android.intent.category.LAUNCHER"/> 
    </intent-filter> 
    <meta-data android:name="target device" android:value="universal"/> 
</activity> 

我Robolectric測試:

@RunWith(RobolectricTestRunner.class) 
public class HomeActivityTest { 

    @Test 
    public void testActionBarDisplay() { 
     // Given 
     final HomeActivity activity_under_testing = new HomeActivity(); 

     // When 
     activity_under_testing.onCreate(null); 

     // Then 
     assertThat(activity_under_testing.getActionBar()) 
     .hasDisplayOptions(ActionBar.DISPLAY_SHOW_HOME 
          |ActionBar.DISPLAY_SHOW_TITLE 
          |ActionBar.DISPLAY_USE_LOGO); 
    } 
} 

我使用FEST-的Android爲assertThat(不可能成爲問題)。

問題:

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 7.563 sec <<< FAILURE! testActionBarDisplay(com.imminentmeals.android.base.ui.HomeActivityTest) Time elapsed: 6.588 sec <<< FAILURE! java.lang.AssertionError: expecting actual not to be null at com.imminentmeals.android.base.ui.HomeActivityTest.testActionBarDisplay(HomeActivityTest.java:28)

我要補充一點,我使用Robolectric 2.0阿爾法2。我跳過了測試,並打包了我的apk,並且ActionBar似乎正常工作,所以我覺得這一定是Robolectric問題?但我認爲它應該可以在原生Android上運行,因爲它運行的是原生Android代碼。

回答

0

在您的清單中,您應該製作主題Theme.Sherlock而不是Theme.Holo

您需要延長SherlockActivitySherlockFragmentActivity

當您使用ABS時返回ActionBar請致電getSupportActionBar

此外,您不需要撥打Window.FEATURE_ACTION_BAR。使用Holo主題就足夠了。

從ActionBarSherlock文檔

Action Bar API

When creating an activity to use the action bar on all versions of Android, you must declare your activity to extend any of the activity classes that start with 'Sherlock' (e.g., SherlockActivity, SherlockFragmentActivity). Interaction with the action bar is handled by calling getSupportActionBar() (instead of getActionBar()).

You can read the docs here

You can read more about styling ABS here

+0

再次檢查我的問題,我不* *不再使用ABS,我切換到框架,我minSDK是14.另外,Window.FEATURE_ACTION_BAR不會傷害任何東西,而且是有,因爲它在之前對類似問題的回答中已有陳述,儘管我認識到這不是必要的。我一直在嘗試我能找到的大部分東西。 – 2013-03-23 05:13:05

+0

啊,我的不好。我誤解了,並認爲你想切換到ABS。 – adneal 2013-03-23 05:23:22

+0

明白了,我重新編寫了它,以免再次發生 – 2013-03-23 05:33:10

4

這個問題是比較老了,但我會後我的,以防有人解決面臨同樣的問題。

我正面臨着與Dandre相同的問題,我在那裏使用Robolectric 2.3,minSDK 16,並使用原生ActonBar,但getActionBar()保持returnign null。解決方案是確保我的風格指的是明確提及ActionBar的主題。在我的情況下,我使用Theme.Light。通過改變主題爲Theme.Holo.Light.DarkActionBar,我得到它的工作。我相信Dandre也有同樣的問題,因爲他的主題是Theme.Holo

我覺得很奇怪,主題是問題的原因,因爲它不影響我的應用程序。也許有人可以解釋爲什麼?

如果使用ActionBarDrawerToggle
+0

感謝分享,我相信我已經刪除了我的主題名稱的具體內容,但是運行時導致可見ActionBar的任何主題都不應返回「null」 Robolectric世界。 – 2014-01-02 05:00:59

+0

這就是我的想法,因爲即使使用Theme.Light,我的應用程序也有可見的操作欄。我從來沒有想過這會是我的問題的原因 – wschang 2014-01-02 07:02:32

+0

我有一個類似的問題,並在嘗試調試時多次查看我的主題,這對我來說似乎是正確的。讀完這個答案後,我仔細看了看自己的主題,意識到自己會看到我認爲應該在那裏的東西,而不是實際存在的東西。 *嘆了口氣*這麼多浪費時間。 – DavidsAlias 2015-01-22 03:31:44

1

把getActionBar()onDrawerOpened(查看drawerView)內{} public void onDrawerOpened(View drawerView) { title = getActionBar().getTitle(); getActionBar().setTitle("Open Drawer"); }

1

Extend ActionBarActivity in place of FragmentActivity.

  • ActionBarActivity向內延伸FragmentActivity,所以沒有必要擔心。
  • 使用android.support.v7類型的ActionBar活動。

    import android.support.v7.app.ActionBarActivity; 
    

Use getSupportActionBar() in place of getActionBar()

  • 檢查你的清單文件,如果使用NoTitleBar主題,然後刪除該行

android:theme="@android:style/Theme.NoTitleBar" // remove this line

  • 如果仍然不解決問題,然後使用FEATURE_ACTION_BAR在setContentView()方法之上。

requestWindowFeature(Window.FEATURE_ACTION_BAR);

+0

嗯,這可能有效,但現在已經很老了,我沒有任何舊代碼。 – 2015-04-05 05:47:35