获取java.lang.NullPointerException甚至该值不为null

时间:2015-10-17 12:20:38

标签: java android preferenceactivity

我有片段,因为我有一个方法

private void update() {
        NetworkingTask task = new NetworkingTask();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String inputValue = sharedPreferences.getString(getString(R.string.pref_key),
                getString(R.string.pref_defualt));
        task.execute(inputValue, null, null);
    }

    @Override
    public void onStart() {
        super.onStart();
        update();
    }

上面的代码工作完美意味着使用

获得正确的结果
sharedPreferences.getString(getString(R.string.pref_key),
                    getString(R.string.pref_defualt));

pref_general.xml文件是

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">   

    <EditTextPreference
        android:key="@string/pref_defualt"
        android:title="@string/pref_label"
        android:defaultValue="@string/pref_default_display_name"
        android:selectAllOnFocus="true"
        android:inputType="textCapWords"
        android:capitalize="words"
        android:singleLine="true"
        android:maxLines="1" />      

</PreferenceScreen>

但在PreferenceActivity

public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);

        String prefs_key = getString(R.string.pref_key);

        Log.e("PREFS_KEY_VALE", "Prefs key have " + prefs_key + " as Value");// even here the value is not null

        if (prefs_key != null) {
            bindPreferenceSummaryToValue(findPreference(prefs_key));// Here comes NPE
        }    
    }

    public void bindPreferenceSummaryToValue(Preference preference){
        preference.setOnPreferenceChangeListener(SettingsActivity.this);

        onPreferenceChange(preference, PreferenceManager
                .getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference){
            ListPreference listPreference = (ListPreference) preference;

            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0){
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        }else {
            preference.setSummary(stringValue);
        }

        return true;
    }
} 

日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ali.arshad.rhcloud.com.sunshine/ali.arshad.rhcloud.com.sunshine.SettingsActivity}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                at android.app.ActivityThread.access$600(ActivityThread.java:123)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4424)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at ali.arshad.rhcloud.com.sunshine.SettingsActivity.bindPreferenceSummaryToValue(SettingsActivity.java:30)
                at ali.arshad.rhcloud.com.sunshine.SettingsActivity.onPostCreate(SettingsActivity.java:23)
                at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1112)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                at android.app.ActivityThread.access$600(ActivityThread.java:123)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4424)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                at dalvik.system.NativeStart.main(Native Method)

我无法调试为什么NPE会来那里!

0 个答案:

没有答案