当前位置 : 主页 > 手机开发 > android >

android – SharedPreferences ListPreference NullPointerException

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在尝试在我的首选项xml中设置频率列表,但我一直收到此错误.我也附上了所有相关文件.我相信我正确设置了值,但我找不到我的错误当前的位置.另外,当我使用SharedPreferences.getString
我正在尝试在我的首选项xml中设置频率列表,但我一直收到此错误.我也附上了所有相关文件.我相信我正确设置了值,但我找不到我的错误当前的位置.另外,当我使用SharedPreferences.getString(key,defaultValue)时返回什么值? ENTRY或ENTRYVALUE?

我的错误:

08-20 00:14:25.195: E/AndroidRuntime(1260): FATAL EXCEPTION: main
08-20 00:14:25.195: E/AndroidRuntime(1260): java.lang.NullPointerException
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.ListPreference.findIndexOfValue(ListPreference.java:215)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.ListPreference.getValueIndex(ListPreference.java:224)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.ListPreference.getEntry(ListPreference.java:202)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.ListPreference.getSummary(ListPreference.java:148)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.Preference.onBindView(Preference.java:515)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.Preference.getView(Preference.java:453)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:222)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.AbsListView.obtainView(AbsListView.java:2461)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.ListView.makeAndAddView(ListView.java:1775)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.ListView.fillDown(ListView.java:678)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.ListView.fillFromTop(ListView.java:739)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.ListView.layoutChildren(ListView.java:1628)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.AbsListView.onLayout(AbsListView.java:2296)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.View.layout(View.java:14063)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewGroup.layout(ViewGroup.java:4603)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.View.layout(View.java:14063)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewGroup.layout(ViewGroup.java:4603)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.View.layout(View.java:14063)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewGroup.layout(ViewGroup.java:4603)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.View.layout(View.java:14063)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewGroup.layout(ViewGroup.java:4603)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.View.layout(View.java:14063)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewGroup.layout(ViewGroup.java:4603)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1994)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1815)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4518)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.Choreographer.doFrame(Choreographer.java:525)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.os.Handler.handleCallback(Handler.java:615)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.os.Looper.loop(Looper.java:137)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at android.app.ActivityThread.main(ActivityThread.java:4898)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at java.lang.reflect.Method.invoke(Method.java:511)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-20 00:14:25.195: E/AndroidRuntime(1260):     at dalvik.system.NativeStart.main(Native Method)

xml布局:

<ListPreference 
        android:key="frequency_key"
        android:title="Sample Rate"
        android:defaultValue="8000"
        android:entries="@array/freq_titles"
        android:entryValues="@array/freq_values"
        />

xml数组:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="freq_titles">
    <item name="8000">8k Hz</item>
    <item name="16000">16k Hz</item>
    <item name="22050">22.05k Hz</item>
    <item name="44100">44.1k Hz</item>
    <item name="48000">48k Hz</item>
</string-array>

<array name="freq_values">
    <item name="8000">8000</item>
    <item name="16000">16000</item>
    <item name="22050">22050</item>
    <item name="44100">44100</item>
    <item name="48000">48000</item>
</array>

</resources>

更新:即使我从默认的共享首选项中执行getAll(),它也很奇怪,它向我显示“frequency_key = 8k Hz”.所以它已经初始化了.

来自我的偏好类的更多代码:

public class Settings_ActivityAH extends PreferenceFragment implements OnSharedPreferenceChangeListener{
private SharedPreferences prefs;
private Preference myAccountName;
private Preference myGoogleUsername;
private Preference myGooglePassword;
private static Context context;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    context = getActivity();

    if (context == null){
        Log.e("error","context is null");
    }


    prefs = PreferenceManager.getDefaultSharedPreferences(context);

    myAccountName = (Preference) findPreference("account_name_title_key");
    myGoogleUsername = (Preference) findPreference("googleusername_key");
    myGooglePassword = (Preference) findPreference("googlepassword_key");
    myAccountName.setSummary(prefs.getString("account_name_title_key", "none"));
    myGoogleUsername.setTitle("username: "+prefs.getString("googleusername_key", "none"));
    myGooglePassword.setTitle("password: "+prefs.getString("googlepassword_key", "none"));
    Log.e("Check prefs",prefs.getAll().toString());
    prefs.registerOnSharedPreferenceChangeListener(this);

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    Log.e("in onSharedPref","yea");
    Preference preference = findPreference(key);

    if (preference.getClass() == EditTextPreference.class){
        Log.e("in editText Pref", "yeah");

        if( key.equals("account_name_title_key")){
            myAccountName.setSummary(sharedPreferences.getString("account_name_key","none"));
            Log.e("accountname prefs",sharedPreferences.getString("account_name_key","none"));
    }
        if( key.equals("account_name_key")){
            myAccountName.setSummary(sharedPreferences.getString("account_name_key","none"));
            Log.e("accountname prefs",sharedPreferences.getString("account_name_key","none"));
    }

    else if (preference.getClass() == PreferenceCategory.class){
        Log.e("in category Pref", "yeah");

        if (key.equals("googleusername_key")){
            myGoogleUsername.setTitle("Username: "+sharedPreferences.getString(key, "none"));
            Log.e("googleusername prefs",sharedPreferences.getString(key, "none"));

        }
        else if( key.equals("googlepassword_key")){
            myGooglePassword.setTitle("Password: "+sharedPreferences.getString(key, "none"));
            Log.e("googlepassword prefs",sharedPreferences.getString(key, "none"));

        }
    }

}
else{
    Log.e("insufficient key called","onSharedPreferenceChanged AH");
}

}

}

似乎Android只接受字符串数组作为ListPreference XML属性android:entryValues的参数.这与所有XML属性仅采用具有一种特定的预定类型(即int,bool,string等)的参数一致.请参阅以下问题: ListPreference: use string-array as Entry and integer-array as Entry Values doesn’t work

关于你的问题,getString()将返回与被点击的条目对应的android:entryValues数组的元素. android:entries数组仅用作用户的一系列显示字符串,并且在代码中没有其他影响,而android:entryValues中的值用于应用程序中的持久存储.

网友评论