getActionBar() returns null in PreferenceActivity (AppCompat-v7 21)

后端 未结 4 2023
离开以前
离开以前 2020-12-09 16:12

I have implemented DoneBar (two buttons in actionbar) in PreferenceActivity as provided in v20 sdk samples, but after updating SDK and AppCompat to version 21 m

相关标签:
4条回答
  • 2020-12-09 16:18

    Issue resolved by doing this, your styles xml should looks like this.

    res/values/style.xml

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowDisablePreview">true</item>
        <item name="android:windowActionBar">true</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    

    res/values-v11/style.xml

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light"> 
            <!-- API 11 theme customizations can go here. -->
        </style>
    

    res/values-v14/style.xml

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light"> 
            <!-- API 11 theme customizations can go here. -->
        </style>
    
    0 讨论(0)
  • 2020-12-09 16:19

    getActionBar() returns null in API 5.0 when i use Activity or FragmentActivity.

    I solved this extendig to ActionBarActivity

    public class MainActivity extends ActionBarActivity {
    

    You must use getSupportActionBar() when you are using ActionBarActivity (appcompat-v7).

    0 讨论(0)
  • 2020-12-09 16:31

    I managed to fix this issue by specifying custom theme for my settings activity,

    <style name="SettingsTheme" parent="style/Theme.AppCompat.Light">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
    </style>
    

    and : android:theme="@style/SettingsTheme" in manifest for activity

    actionbar is again showing on KITKAT and LOLIPOP and (have not tested it) back to api v11. I tested it and it works (with no actionbar as expected) on api 10.

    From debugging on lolipop, FEATURE_NO_TITLE was being set in PhoneWindow.java:3243 :

       if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
            requestFeature(FEATURE_NO_TITLE);
    

    which removes FEATURE_ACTION_BAR

    [edit]

    but this action bar is not from material theme, so still its not perfect

    [edit2]

    I gave up with Headers, now I am using PreferenceFragment backport from github. Now all my actionbars are the same after upgrade to appcompact 21.

    0 讨论(0)
  • 2020-12-09 16:34

    as xXx requested I am providing example how I done:

    public class SettingsActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Display the fragment as the main content.
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new SettingsFragment())
                    .commit();
    
            // use action bar here
            ActionBar actionBar = getSupportActionBar();
        }
    
        public static class SettingsFragment extends PreferenceFragment {
            @Override
            public void onCreate(Bundle paramBundle) {
                super.onCreate(paramBundle);
                addPreferencesFromResource(R.xml.pref_settings);
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题