Android:OnSharedPreferenceChangeListener抛出NullPointerException

时间:2011-08-10 15:43:12

标签: android nullpointerexception sharedpreferences

我很难为什么在注册这个监听器时遇到这个异常,我找不到它。

问题似乎是prefs.registerOnSharedPreferenceChangeListener(listener);,但我找不到解决方案!

代码如下:

public class MtprojectActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
[...]
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs,
                    String key) {
                if (key.equals("CALLS_FREQUENCY_PREF")) {
                    newCallsFrequencyValue = Integer.parseInt(prefs.getString(Preferences.CALLS_FREQUENCY_PREF, "0"));

                } else if (key.equals("SMS_FREQUENCY_PREF")) {
                    newSmsFrequencyValue = Integer.parseInt(prefs.getString(Preferences.SMS_FREQUENCY_PREF, "0"));

                } else if (key.equals("LOC_FREQUENCY_PREF")) {
                    newLocationFrequencyValue = Integer.parseInt(prefs.getString(Preferences.LOCATION_FREQUENCY_PREF, "0"));
                }
            }
        };

        prefs.registerOnSharedPreferenceChangeListener(listener);

这是异常追踪:

08-10 15:31:39.537: ERROR/AndroidRuntime(547): FATAL EXCEPTION: main
08-10 15:31:39.537: ERROR/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.mtproject/app.mtproject.MtprojectActivity}: java.lang.NullPointerException
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.os.Looper.loop(Looper.java:130)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at java.lang.reflect.Method.invoke(Method.java:507)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at dalvik.system.NativeStart.main(Native Method)
08-10 15:31:39.537: ERROR/AndroidRuntime(547): Caused by: java.lang.NullPointerException
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at app.mtproject.MtprojectActivity.onCreate(MtprojectActivity.java:88)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-10 15:31:39.537: ERROR/AndroidRuntime(547):     ... 11 more

提前多多感谢!

2 个答案:

答案 0 :(得分:0)

好的,只需确保在包含从活动中检索到的prefs之前已正确调用变量SharedPreferences <)

答案 1 :(得分:0)

我认为您必须将课程与OnSharedPreferenceChangeListener

联系起来