2

i am getting this problem and do not know how to find this out and I am also adding code where I getting this

  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)

My code:

 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 );
    }

now please help me out for setting this problem

Sufian
  • 6,405
  • 16
  • 66
  • 120
Junaid
  • 316
  • 5
  • 23

1 Answers1

1

I think it because MainApplication.getInstance() is null. maybe you forgot to assign value for MainApplication.getInstance() or onPreferenceChange was called before the assigned code.

justHooman
  • 3,044
  • 2
  • 17
  • 15
  • Yep. Looking at the crashing code, the only thing that could throw a `NullPointerException` would be `MainApplication.getInstance()` returning `null`: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/preference/PreferenceManager.java#L373 – Albin Jun 03 '15 at 07:46