PreferenceFragment.findPreference 总是返回 NULL [英] PreferenceFragment.findPreference always returns NULL
问题描述
我目前正在尝试制作一个设置菜单,它将显示一个MultiSelectListPreference
,以从您的联系人列表中选择多个联系人.
I'm currently trying to make a settings menu, that will show a MultiSelectListPreference
, to select multiple contacts from your contact list.
此时,我收到 NullPointerException
,当我尝试 MultiSelectListPreference#setEntryValue(CharSequence[])
如果我把 setEntries
代码> 首先,那个抛出相同的异常.
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.
我已经设置了一个断点,以逐步查看会发生什么.变量被填充是因为它们存储Strings
,它们可以包含一个String
null",所以我猜如果没有Display_Name<,它不会失败/code> 可用左右.
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.
我基于 这个答案
有人有想法吗?如果您需要更多信息,请告诉我.感谢阅读!
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();
}
}
}
好的,我又做了几次检查.-我在我的 XML 中做了一个测试首选项,并使用 findPrefence 使它的一个对象可以使用 -> 返回 NULL- 我已经将我的 MultiSelectListPreference 的键设置为 @string/test,把它放在我的 strings.xml 中,findpreference 仍然返回 Null.
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.
我的 PreferenceFragment 有问题吗?
Can there be a problem with my PreferenceFragment?
推荐答案
好的,我找到了我的问题所在.
Ok, i found what my problem was.
MultiSelectListPreference mslp = (MultiSelectListPreference) findPreference("contactList");
返回NULL,因为
addPreferencesFromResource(R.xml.preferences);
一开始还没有完成......所以它还没有加载我的偏好.
is not done at the start... so it didn't load my preferences in yet.
这篇关于PreferenceFragment.findPreference 总是返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!