Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

java - PreferenceFragment.findPreference始终返回NULL(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.

(我目前正在尝试创建一个设置菜单,该菜单将显示MultiSelectListPreference ,以从您的联系人列表中选择多个联系人。)

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.

(此刻,当我尝试MultiSelectListPreference#setEntryValue(CharSequence[])时,我将收到NullPointerException异常。如果我将setEntries放在首位, setEntries引发相同的异常。)

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.

(变量被填充是因为它们存储Strings ,它们可以包含String “ null”,所以我猜如果没有可用的Display_Name ,它不会失败。)

I based the findPreference on the example of this answer

(我基于此答案的示例findPreference)

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.

(-我在XML中设置了测试首选项,并使用findPrefence使其成为可使用的对象->返回NULL-我将MultiSelectListPreference的键设置为@ string / test,并将其放置在strings.xml中,findpreference仍然返回Null。)

Can there be a problem with my PreferenceFragment?

(我的PreferenceFragment可能有问题吗?)

  ask by Wesley De Keirsmaeker translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, i found what my problem was.

(好的,我发现了我的问题所在。)

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

returns NULL because

(返回NULL,因为)

addPreferencesFromResource(R.xml.preferences);

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

(一开始还没有完成...所以它还没有加载我的偏好设置。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...