PreferenceFragment.findPreference always returns NULL

时光总嘲笑我的痴心妄想 提交于 2019-12-18 04:03:29

问题


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?


回答1:


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.




回答2:


You can solve this using

getFragmentManager().executePendingTransactions();

before

findPreference(section);



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/14339550/preferencefragment-findpreference-always-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!