getActionBar() returns null

前端 未结 24 1229
暗喜
暗喜 2020-11-22 13:31

I\'m having an odd problem. I am making an app with targetsdk 13.

In my main activity\'s onCreate method i call getActionBar() to setup my actionbar. T

相关标签:
24条回答
  • 2020-11-22 13:32

    The main reason for that is using themes that are not supporting ActionBar:

    In manifest file add the following either in your target activity or application element (if you want to unify the theme over whole application)

    Examples of themes that are supporting action bar "Theme.AppCompat.Light" or "Theme.Holo.Light" ...

    android:theme="@android:style/Theme.Holo.Light"
    

    It is better to put all styles in styles.xml and use it everywhere using "@style/themName" so the previous one will be

    android:theme="@style/AppTheme"
    

    and styles.xml will have the following:

     <style name="AppTheme" parent="Theme.AppCompat.Light">
    

    Hints:

    • There is some themes that can not be used in old SDKs like "@android:style/Theme.Holo.Light.DarkActionBar" is not supported before SDKs version 14.
    • To allow your app to support minimum specific version of SDK you could add the following under <app> element:

      <uses-sdk android:minSdkVersion="14" />
      
    • To specify min SDK version in AndroidStudio, you could by using app's Gradle file.

      android{
        defaultConfig{
          minSdkVersion 14
          targetSdkVersion 21
        }
      }
      
    0 讨论(0)
  • 2020-11-22 13:33

    ActionBar needs application or activity's Theme to have an app title. Make sure you have not styled your application or activity as Theme.NOTITLE.

    <application
        android:name="com.xxx.yyy"
        android:debuggable="false"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code
    
    
    <activity
            android:name="com.xxx.yyy.Activity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/Theme.NoTitle"  // remove this line if you have in your code
            android:windowSoftInputMode="adjustResize|stateHidden" > 
    
    0 讨论(0)
  • 2020-11-22 13:33

    I ran into this problem . I was checking for version number and enabling the action bar only if it is greater or equal to Honeycomb , but it was returning null. I found the reason and root cause was that I had disabled the Holo Theme style in style.xml under values-v11 folder.

    0 讨论(0)
  • 2020-11-22 13:33

    Try extending your Activity class from ActionBarActivity. This solved it for me. Do something like the following:

    public class MyActivity extends ActionBarActivity
    {
      . . .
    

    In my case the class was extending only from Activity.

    0 讨论(0)
  • 2020-11-22 13:34

    I faced the above issue where getActionBar() method returns null. I was calling the getActionBar() after setting the setContentView() and still its returning a null.

    I resolved the issue by setting the min-sdk version in Android Manifest file that was missing initially. <uses-sdk android:minSdkVersion="11" />

    0 讨论(0)
  • 2020-11-22 13:38

    Can use getSupportActionBar() instead of getActionBar() method.

    0 讨论(0)
提交回复
热议问题