共享首选项正在通过异常

时间:2015-06-03 07:14:12

标签: android

我遇到了这个问题,不知道如何找到这个问题,我也在添加代码,我得到了这个

  Caused by: java.lang.NullPointerException
            at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:376)
            at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:371)
            at com.example.mine4.accelerometergpsmaster.MainApplication.onPreferenceChange(MainApplication.java:100)
            at com.example.mine4.accelerometergpsmaster.MainActivity.onResume(MainActivity.java:108)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
            at android.app.Activity.performResume(Activity.java:5200)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2931)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2973)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2408)
            at android.app.ActivityThread.access$600(ActivityThread.java:165)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

我的代码:

 public static void onPreferenceChange() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApplication.getInstance());
        //  Get latest settings, and update accordingly
        boolean newState = sp.getBoolean("pref_onoff", false); // false is off/not-running

        prefInterval = Integer.parseInt( sp.getString("pref_interval", "60") );
        prefTimeout = Integer.parseInt( sp.getString("pref_timeout", "30") );

        MainApplication.prefThreshold = Double.parseDouble( sp.getString("pref_threshold", "0.30") );
        MainApplication.prefThreshold2 = MainApplication.prefThreshold + 9.80;

        // If we turned off the service, handle that change
        toggleState( newState );
    }

现在请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

我认为是因为MainApplication.getInstance()为null。 也许你忘了为MainApplication.getInstance()分配值,或者在分配的代码之前调用onPreferenceChange。