0

I am trying to use ListPreference item in a Fragment.
I have the following Fragment:

public class PreferencesFragment extends PreferenceFragment
{

    public PreferencesFragment() {
    }


    public static PreferencesFragment newInstance() {
        PreferencesFragment fragment = new PreferencesFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

}

My preferences.xml looks as follows:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <ListPreference
        android:title="Test"
        android:key="list_contacts"
        android:defaultValue="0"
        android:entries="@array/list_entries"
        android:entryValues="@array/list_values"/>
</PreferenceScreen>

and my arrays.xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="list_entries">
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </string-array>

    <string-array name="list_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

I am attaching that PreferencesFragment into my MainActivity.
It shows up, but when I click on that item, the dialog with list of items doesn't show up.

Instead I am getting the following error output:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
 at com.android.internal.app.AlertController.setupView(AlertController.java:472)
 at com.android.internal.app.AlertController.installContent(AlertController.java:237)
 at android.app.AlertDialog.onCreate(AlertDialog.java:423)
 at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
 at android.app.Dialog.show(Dialog.java:293)
 at android.preference.DialogPreference.showDialog(DialogPreference.java:319)
 at android.preference.DialogPreference.onClick(DialogPreference.java:277)
 at android.preference.Preference.performClick(Preference.java:994)
 at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:214)
 at android.widget.AdapterView.performItemClick(AdapterView.java:310)
 at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
 at android.widget.AbsListView$PerformClick.run(AbsListView.java:3126)
 at android.widget.AbsListView$3.run(AbsListView.java:4041)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6077)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

and the applications stops.

What am I doing incorectly? How can I fix it up?

I have already looked through all threads here, but nothing worked and nothing more comes to my mind.

EDIT: My fragments switching in Activity is quite complex, but all in all it is something like this:

private void navigatePreferences() {
    preferencesFragment = PreferencesFragment.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.mainFragmentPlace, preferencesFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
michalsol
  • 752
  • 8
  • 29
  • `What am I doing incorectly?` You're using a not instantiated object. `How can I fix it up?` Find it and instantiate it before using it. – Phantômaxx Aug 10 '16 at 16:29
  • 1
    @HardikParmar, what do you mean by "do I have store on location"? I don't understand this question. – michalsol Aug 10 '16 at 16:35
  • @Rotwang, Yes, I know the view is null somewhere during showDialog method, but I have no idea why is that so. Do you have any tips? – michalsol Aug 10 '16 at 16:37
  • `newInstance` calls the static method that returns the `new PreferencesFragment()`. Not sure what that would change. – OneCricketeer Aug 10 '16 at 16:39
  • @Rotwang, No, it didn't help. I am getting the same error. (I've even checked that before asking question :) ) – michalsol Aug 10 '16 at 16:40
  • You aren't using the support library, right? I ask because you are using `getFragmentManager` instead of `getSupportFragmentManager` – Phantômaxx Aug 10 '16 at 16:49
  • @Rotwang, I use support library for some stuff (like SwipeToRefresh or ActionBar), but all fragments-related things are implemented without using support library. – michalsol Aug 10 '16 at 16:54
  • Really? then I guess we have the culprit. `all fragments-related things are implemented without using support library` Once you use the support library in your project, you don't decide *which parts* of your app are under its control. Use it or not. You *can't mix* between support and non-support. – Phantômaxx Aug 10 '16 at 17:00
  • @Rotwang, What do you mean? Should I use support library? PreferenceFragment extends Fragment from android.app, not from android.support.v4, how can I use support library for that? – michalsol Aug 10 '16 at 17:05
  • https://developer.android.com/reference/android/support/v14/preference/PreferenceFragment.html and https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.html – Phantômaxx Aug 10 '16 at 17:09
  • @Rotwang, Ok, I've changed `PreferenceFragment` to `Android.support.v14.preference.PreferenceFragment`, and in preferences.xml I've changed to `android.support.v7.preference.PreferenceScreen` and `android.support.v7.preference.ListPreference` but I'm still getting the same error. I must confess I'm really confused now – michalsol Aug 10 '16 at 17:55
  • Are you now using `getSupportFragmentManager`? – Phantômaxx Aug 10 '16 at 17:57
  • @Rotwang, No, I'm still using `getFragmentManager`, because `Android.support.v14.preference.PreferenceFragment` still extends `Fragment` from `android.app`, not from `android.support.v4`, so I can't use `getSupportFragmentManager`. BTW: Thank you for trying to help! I'm really impressed with your stubbornness. – michalsol Aug 10 '16 at 18:02
  • Professional deformation... ;) – Phantômaxx Aug 10 '16 at 18:06
  • By the way, Daniel Nugent's comment on this post seems promising. http://stackoverflow.com/questions/36145647/how-to-go-from-an-activity-to-preferencefragment. – Phantômaxx Aug 10 '16 at 18:12
  • @Rotwang, When I've switched to `PreferenceFragmentCompat` everything works fine (the fragment shows up, and causes no errors when clicked on PrefenceList). But it also means I have to use `getSupportFragmentManager`, and therefore `android.support.v4` in every Fragment. My minSdkVersion is 15, do I really need to use `android.support.v4.Fragments`? – michalsol Aug 10 '16 at 18:50
  • Depending on which is your targetSdkVersion, you might not need to use support at all. If you need o use it, then it's **for all** your app. Is it such a big concern? – Phantômaxx Aug 10 '16 at 19:14
  • @Rotwang, But you see, that without support Fragments I can't make PreferenceFragment work. I obviously can't resign from support libraries completely (e.g. because of `SwipeRefreshLayout` which is not available without support libraries). I can't understand why my solution presented in question didn't work. What does the use of support fragments change here? – michalsol Aug 10 '16 at 19:18
  • Once you use the support library, you have to use in all your app. It simply adds the missing features to the older API levels. So, you could choose not to support the older devices OR not to use the newest features. But I don't see why don't you resign to use the support. It's not harmful by any means. AND, most importantly, your app now works. – Phantômaxx Aug 10 '16 at 20:03
  • @Rotwang Which features am I missing in API 15+ that I have to use support fragments? Why do you say I must use support libs everywhere if I had used it just once? I didn't hear it before. As far as I know, we should use support libs only there, where we are limited with our API level. But in this case all classess are available in my API level, but it still doesn't work. Why? :) I really do not have problem with using support libraries, but now I just doesn't understand why my preferences don't work without it. – michalsol Aug 10 '16 at 22:09
  • I don't know which is your targetSdk, nor the "newer features" you are using which are not natively present in the older APIs. You could try lowering the TARGET sdk and see if you can get rid of the support, if it does really bother you. In my case, I don't even make a problem of that, just use support and live happily with it. – Phantômaxx Aug 11 '16 at 06:51

0 Answers0