preferenceFragment.find preference总是返回NULL [英] PreferenceFragment.findPreference always returns NULL

查看:1097
本文介绍了preferenceFragment.find preference总是返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我想要做一个设置菜单,将显示一个 MultiSelectList preference ,选择从联系人列表中选择多个联系人。

I'm currently trying to make a settings menu, that will show a MultiSelectListPreference, to select multiple contacts from your contact list.

在这一刻,我收到一个 NullPointerException异常,当我尝试 MultiSelectList preference#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.

我已经把断点,看一步一步会发生什么。这些变量都充满因为它们存储字符串,它们可以包含一个字符串空,所以我想这没有关系T失败,如果没有显示名称可用左右。

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.

我的基础上的<一个例子查找preference href="http://stackoverflow.com/questions/6136770/android-dynamic-array-list$p$pference/6137076#6137076">this回答

任何人有一个想法?如果您需要了解更多信息,请告诉我。感谢您的阅读!

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做了一个试验preference并用查找prefence做它的一个目标一起工作 - >返回NULL - 我已经设置了我的MultiSelectList preference我的钥匙串@ /测试,在我的strings.xml推这个,找到preference仍然返回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,因为

returns NULL because

addPreferencesFromResource(R.xml.preferences);

在一开始不这样做......所以也没加载我的preferences问题解答

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

这篇关于preferenceFragment.find preference总是返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆