0

I switched from supporting Gingerbread, since Play no longer supports it. I can now stop using support classes, and move to real ones. Except getActionBar is null

  • Android marshmallow
  • My Theme includes ActionBar (@Style.AppTheme)
  • extending Activity, not ActionBarActivity or AppCompatActivity
  • tried requestWindowFeature(Window.FEATURE_ACTION_BAR); before setContentView, and even before super.onCreate
  • calling getActionBar in onCreate after setContentView

styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

    <style name="mp_name">
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">20sp</item>
    </style>
    <style name="search_date">
        <item name="android:textStyle">italic</item>
        <item name="android:textSize">14sp</item>
    </style>
    <style name="search_details">
        <item name="android:textStyle">normal</item>
        <item name="android:textSize">10sp</item>
    </style>
</resources>

styles.xml (v11)

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!-- API 11 theme customizations can go here. -->
    </style>

</resources>

styles.xml (v14)

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

</resources>

styles.xml (v21)

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>
f.khantsis
  • 3,256
  • 5
  • 50
  • 67
  • 1
    AppTheme extends which Theme? – Anurag Singh Feb 21 '17 at 05:15
  • Check this http://stackoverflow.com/questions/6867076/getactionbar-returns-null – Farmer Feb 21 '17 at 05:20
  • You should keep using AppCompat. Even if you were API 21+ and could use the material themes, you'd still want to use it (there are changes even at API 23 that AppCompat takes care of for you). – ianhanniballake Feb 21 '17 at 05:21
  • 2
    use `AppCompatActivity` and `getSupportActionBar()` – Manohar Feb 21 '17 at 05:22
  • I checked all the solutions there, @Shailesh The whole point of switching to min sdk 14, is I no longer need to use AppCompat and supprotaction bar, i can use the real deal – f.khantsis Feb 21 '17 at 05:24
  • @AnuragSingh AppTheme inherits from Theme.AppCompat.Light.DarkActionBar – f.khantsis Feb 21 '17 at 05:25
  • Share your `style` xml code and java code which you are using to get `actionbar`. – Farmer Feb 21 '17 at 05:25
  • OK, I put style code in the original question. The java code to get actionbar is irrelevent, I commented it out, and now the activity runs -- without an ActionBar. – f.khantsis Feb 21 '17 at 05:34
  • Switching back to AppCompatActivity brought the actionbar back, but I want to use regular activities – f.khantsis Feb 21 '17 at 05:49
  • Check this that how to use default theme from API 10 to API 21 http://stackoverflow.com/questions/9832114/how-to-use-device-default-theme-for-app – Farmer Feb 21 '17 at 05:56

3 Answers3

1

Have you tried getSupportActionBar()? That worked for me when I had a similar issue.

You might also want to check one of the answers on this thread: getActionBar returns null

Community
  • 1
  • 1
Dina
  • 23
  • 6
  • i did not try `getSupportActionBar` since I am not using the support `Activity` class. Also i tried all those solutions, hence the list of things I did in the post – f.khantsis Feb 21 '17 at 05:32
0

Reason being <item name="windowActionBar">false</item>. Remove this and try. No need to use Theme.AppCompat since you are not inheriting AppCompatActivity.

Anurag Singh
  • 6,140
  • 2
  • 31
  • 47
-1
  1. Check you manifest if u have added theme to you activity (if added then remove it).
  2. Check if you have added correct theme to your Application

    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".activity.SplashScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

Abhinav Aggarwal
  • 416
  • 3
  • 14