NullPointerException异常

NullPointerException异常

本文介绍了共享preferences目录preference NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立频率列表在我的preferences XML,但我不断收到此错误。我附上所有相关的文件了。我相信我正确设置了值,但我找不到在哪里我的错误是目前。此外,当我使用共享preferences.getString(键,设置defaultValue)返回什么价值?该条目或ENTRYVALUE?

I'm trying to set up a list of frequencies in my preferences xml but I keep getting this error. I have attached all the related files too. I believe that I set up the values correctly but I can't find where my error currently is. Also, when I use the SharedPreferences.getString(key, defaultValue) what value is returned? The ENTRY or the 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布局:

xml layout:

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

XML数组:

xml array:

<?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()从默认的共享preferences它表明我说:frequency_key = 8K赫兹。因此,它的初始化。

UPDATE: It's weird even when I do a getAll() from the default shared preferences it shows me that "frequency_key=8k Hz". So it's initialized.

这是我的preference类的更多code:

More code from my preference class:

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将只接受字符串数组的作为参数传递给目录preference XML属性安卓entryValues​​ 。这是一个事实,即所有的XML属性只需要一个特定的,predetermined类型(如整型,布尔,字符串,等等)请参见下面的问题论点一致:<一href="http://stackoverflow.com/questions/11346916/list$p$pference-use-string-array-as-entry-and-integer-array-as-entry-values-does">List$p$pference:使用字符串数组作为输入和整数数组项值不起作用

It seems that Android will only accept string-array's as an argument to the ListPreference XML attribute android:entryValues. This is consistent with the fact that all XML attributes take only arguments with one specific, predetermined type (i.e. int, bool, string, etc.) See the following question: ListPreference: use string-array as Entry and integer-array as Entry Values doesn't work

关于你的身边的问题,的getString()将返回安卓的元素:entryValues​​ 数组对应点击进入。该安卓项阵列只作为一个系列的显示字符串的用户,并在code无其他影响,而<$ C $的值C>机器人:entryValues​​ 用于您的应用程序中的持久性存储

About your side question, getString() will return the element of the android:entryValues array corresponding to the clicked entry. The android:entries array is used only as a series of display strings for the user, and has no other impact in code, while the values in android:entryValues are used for persistent storage within your app.

这篇关于共享preferences目录preference NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:49