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

后端 未结 4 2022
离开以前
离开以前 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: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);
            }
        }
    
    }
    

提交回复
热议问题