11

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?

Community
  • 1
  • 1
  • are you sure `findPreference("contactList")` returns real value, not `null`? – Archer Jan 15 '13 at 14:14
  • Didn't tought of that. Damn it. It does indeed return Null. I was looking at the wrong thing. Thanks! – Wesley De Keirsmaeker Jan 15 '13 at 14:30
  • Anyone has an idea why it wouldn't find my preference? The key is right, it's called in the right way (as far as I know). It's in my Fragment. Can it be searching for my preference on the wrong place maybe? – Wesley De Keirsmaeker Jan 15 '13 at 14:38
  • For me it was a stupid mistake. I only added `title` to the `PreferenceCategory` not the key :-(, make sure you also add the key `android:key="pre_item_key" ` and find by the key. – Hossain Khan Sep 01 '19 at 18:54

4 Answers4

27

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.

6

You can solve this using

getFragmentManager().executePendingTransactions();

before

findPreference(section);
Nachi
  • 4,218
  • 2
  • 37
  • 58
luismi
  • 61
  • 1
  • 2
1

In my case, I was trying to use findPreferences in onCreate of the enclosing PreferenceActivity. I moved it down to onCreate of the PreferenceFragment and it works fine.

Mike Miller
  • 3,368
  • 5
  • 36
  • 49
0

You can solve this by placing the all the content access functions inside the following fragment callback

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //Access the content here.
}
Tjaart
  • 496
  • 8
  • 20