我掙扎着爲什麼我註冊這個偵聽器時得到這個異常,我找不到它。Android:OnSharedPreferenceChangeListener拋出空指針異常
這個問題似乎是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
非常感謝提前!
它只是我沒」 t實例化'prefs'變量 – noloman