Android偏好设置NullException

时间:2017-01-22 12:37:48

标签: java android android-preferences

有人可以帮助我吗?

我是Android的乞丐,我不知道我的错在哪里。

提前致谢!

错误记录

01-22 11:45:32.722 13037-13037/com.example.android.sunshine.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.sunshine.app, PID: 13037
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564)
at android.app.ActivityThread.access$800(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5576)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference
at com.example.android.sunshine.app.SettingsActivity.bindPreferenceSummaryToValue(SettingsActivity.java:40)
at com.example.android.sunshine.app.SettingsActivity.onCreate(SettingsActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6041)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564) 
at android.app.ActivityThread.access$800(ActivityThread.java:170) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5576) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 

的strings.xml

<string name="app_name">Sunshine</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="action_refresh" translatable="false">Refresh</string>
<string name="title_activity_detail">Details</string>
<string name="title_activity_settings">Settings</string>


<string name="pref_location_label" >Location</string>

<string name="pref_location_key" translatable="false">location</string>

<string name="pref_location_default" translatable="false">94043</string>

SettingsActivity.java

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.pref_general);

        bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
    }

    private void bindPreferenceSummaryToValue(Preference preference) {

        preference.setOnPreferenceChangeListener(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) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
            // For other preferences, set the summary to the value's simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }

}

这是我的pref_general.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    >

    <EditTextPreference
        android:key="pref_location_key"
        android:title="@string/pref_location_label"
        android:defaultValue="@string/pref_location_default"
        android:inputType="text"
        android:singleLine="true" />
</PreferenceScreen>

我尝试了一些方法来解决这个问题,但没有成功。当按下按钮&#34;设置&#34;然后我的应用关闭并向我显示错误消息时会发生此错误。

1 个答案:

答案 0 :(得分:1)

Look at the image

查看上图,看preference内的bindPreferenceSummaryToValue对象为空。这一行,更准确

preference.setOnPreferenceChangeListener
  

如何调试

  1. 确保findPreference实际上返回了一些东西而不是空值。在那里使用日志声明来查看。
  2. 如果第一步为您提供NULL值,请检查pref_location_key是否确实存在,然后将其传递给bind方法。
  3. 阅读此函数的javadoc。

    findPreference - Javadoc