Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActionBar() == null When it Shouldn't

I feel like I must be missing something crazy. I am converting my code written to work with ABS (and it did work), and switching it to use the native ActionBar. I set the min SDK to 14 and switched things out for the framework versions, now I can't get the ActionBar to exist.

My Activity:

public class HomeActivity extends Activity {

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

My 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>

My Robolectric Test:

@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);
    }
}

I'm using FEST-Android for the assertThat (couldn't possibly be the issue).

The issue:

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)

I should add that I'm using Robolectric 2.0 Alpha 2. I skipped the test, and packaged my apk, and the ActionBar seems to work fine, so I'm feeling like this must be a Robolectric issue? But I thought it should work fine with native Android since it was running native Android code.

like image 354
Dandre Allison Avatar asked May 10 '26 10:05

Dandre Allison


2 Answers

This question is rather old but I will post my solution in case someone faces the same problem.

I was facing the same problem as Dandre, where I was using Robolectric 2.3, minSDK 16, and using the native ActonBar and yet getActionBar() kept returnign null. The solution was to ensure my style was referring to a theme which explicitly mentions ActionBar. In my case,I was using Theme.Light. By changing the theme to Theme.Holo.Light.DarkActionBar, I got it to work. I believe Dandre had the same issue, given that his theme was Theme.Holo.

I find it weird that the theme was the cause of the problem, given that it does not affect my app. Maybe someone can explain why?

like image 58
wschang Avatar answered May 13 '26 00:05

wschang


if you use ActionBarDrawerToggle put getActionBar() inside of onDrawerOpened(View drawerView){} public void onDrawerOpened(View drawerView) { title = getActionBar().getTitle(); getActionBar().setTitle("Open Drawer"); }

like image 29
Mike Avatar answered May 12 '26 22:05

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!