24

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 my app crashes at

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int, int)' on a null object reference

That is because getActionBar() returns null. And There is no getSupportActionBar() as in ActionBarActivity.

So my question is how to get that actionbar object in PreferenceActivity so I could apply custom view on it?

SOLVED

After some research I managed to solve this problem by using PreferenceFragment with ActionBarActivity so I could call getSupportActionBar()

Olegas Gončarovas
  • 1,521
  • 1
  • 11
  • 16
  • 3
    Hey, would it be possible that you create an answer with the solution code in it? I currently have the same issue and cannot really find out what the problem is. –  Oct 19 '14 at 12:09

4 Answers4

18

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.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • 2
    Which is correct. Do not use `Theme.AppCompat` unless your Activity extends `ActionBarActivity`. – Chris Banes Oct 20 '14 at 09:06
  • 2
    @ChrisBanes ok, so if I want actionbar in my preferences then I should use ActionBarActivity with PreferenceFragment. ActionBar in preferenceActivity is deprecetad and since 21 non supported? can you suggest how to implement headers with PreferenceFragment in ActionBarActivity? I currently use loadHeadersFromResource. – marcinj Oct 20 '14 at 09:15
  • I don't understand why this correct answer was down voted (so I up voted for one) – mindex Nov 02 '14 at 09:25
15

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

}
dimsuz
  • 8,969
  • 8
  • 54
  • 88
Olegas Gončarovas
  • 1,521
  • 1
  • 11
  • 16
  • Can I use this with header xml files, which are used in `PreferenceActivity` in `onBuildHeaders()`? – mars3142 Nov 19 '14 at 15:33
  • And what theme did you use? – Jorgesys Nov 25 '14 at 22:04
  • 1
    Note that you can use non-support PreferenceFragment with ActionBarActivity on API > 11, just use getFragmentManager() to add it.Worked for me. For earlier versions of Android I guess the only way is to use some backported PreferenceFragment from github. – dimsuz Nov 26 '14 at 11:49
  • When a nested "PreferenceScreen" is displayed, the action bar is removed. Is there any solution that works with appcompat-v7 21? – Petrakeas Dec 08 '14 at 12:29
7

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).

Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

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>
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151