Wesley De Keirsmaeker Wesley De Keirsmaeker - 1 month ago 8
Android Question

PreferenceFragment.findPreference always returns NULL

I'm currently trying to make a settings menu, that will show a

MultiSelectListPreference
, to select multiple contacts from your contact list.

At this moment, I'm receiving an
NullPointerException
, when i try to
MultiSelectListPreference#setEntryValue(CharSequence[])
If I put the
setEntries
first, that one throws the same exception.

I've put a breakpoint, to see step by step what happens. The variables are filled because they store
Strings
, they can contain a
String
"null", so I guess that it doesn't fail if there is no
Display_Name
available or so.

I based the findPreference on the example of this answer

Anyone has an idea? If you need more information, tell me. Thanks for reading!

package be.wdk.sendtowork;contactNumberArray

import android.database.Cursor;
import android.os.Bundle;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceFragment;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

public class PreferenceClass extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Integer countContacts = 0;

String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI
};

String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER;
String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
try {
Cursor c1 = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection, null, sortOrder);
c1.moveToFirst();
Integer c1columncount = c1.getColumnCount();
Integer c1count = c1.getCount();
Toast toastje = Toast.makeText(getActivity(), c1columncount.toString() + " - " + c1count.toString(), Toast.LENGTH_SHORT);
toastje.show();

CharSequence[] contactNameArray = new CharSequence[c1count], contactNumberArray = new CharSequence[c1count];
MultiSelectListPreference mslp = (MultiSelectListPreference) findPreference("contactList");
do {
contactNameArray[countContacts] = c1.getString(0) + " - " + c1.getString(2);
contactNumberArray[countContacts] = c1.getString(1);
countContacts += 1;
} while(c1.moveToNext());

mslp.setEntryValues(contactNumberArray); //<- line that throws the error
mslp.setEntries(contactNameArray);
addPreferencesFromResource(R.xml.preferences);
}
catch (Exception e) {
Log.v("TAG", " " + e.toString());
e.getMessage();
}
}
}


EDIT: Ok, I did a couple more checks.
-I made a test preference in my XML and used the findPrefence to make an object of it to work with -> returns NULL
-I have set my key of my MultiSelectListPreference to @string/test, putted this in my strings.xml, findpreference still returns Null.

Can there be a problem with my PreferenceFragment?

Answer

Ok, i found what my problem was.

MultiSelectListPreference mslp = (MultiSelectListPreference) findPreference("contactList"); 

returns NULL because

addPreferencesFromResource(R.xml.preferences);

is not done at the start... so it didn't load my preferences in yet.