ANDROID JAVA: Activity doesn't start when the used SharedPreferences -


i have problem. want read value integer of activity "settings" in service gpstracker , use there. use sharedprefrences , confirming key input. if data validated application returns class fullscreenactivity. code responsible in activity settings:

    sharedpreferences.editor editor;     public static final string name = "distance";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_settings);     final view controlsview = findviewbyid(r.id.fullscreen_content_controls);     final view contentview = findviewbyid(r.id.fullscreen_content);      sharedpreferences pref = getapplicationcontext().getsharedpreferences(name, mode_private);     editor=pref.edit();      edit1= (edittext) findviewbyid(r.id.editskan);     accept= (button) findviewbyid(r.id.buttonskan);     accept.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {             if (!edit1.gettext().tostring().equals(""))                 value = integer.parseint(edit1.gettext().tostring());             editor.putint("settings", value);             editor.commit();              toast.maketext(getapplicationcontext(), "changed, value + " m", toast.length_short).show();             finish();         }     }); 

a code in gpstracker this:

      sharedpreferences pref; 

(...)

    private int downloadsettings()     {         pref=context.getsharedpreferences("distance", activity.mode_private);         value = pref.getint("settings",15);           return value;     } 

and method call:

    int dist = downloadsettings(); 

when run apps 2 errors in log:

*java.lang.runtimeexception: unable start activity componentinfo{com.example.adam.mobileproject/com.example.adam.mobileproject.fullscreenactivity}: java.lang.nullpointerexception             @ android.app.activitythread.performlaunchactivity(activitythread.java:2184)             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2233)             @ android.app.activitythread.access$800(activitythread.java:135)             @ android.app.activitythread$h.handlemessage(activitythread.java:1196)             @ android.os.handler.dispatchmessage(handler.java:102)             @ android.os.looper.loop(looper.java:136)             @ android.app.activitythread.main(activitythread.java:5001)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601)             @ dalvik.system.nativestart.main(native method)      caused by: java.lang.nullpointerexception             @ com.example.adam.mobileproject.gpstracker.downloadsettings(gpstracker.java:284)             @ com.example.adam.mobileproject.gpstracker.<init>(gpstracker.java:200)             @ com.example.adam.mobileproject.fullscreenactivity.oncreate(fullscreenactivity.java:40)             @ android.app.activity.performcreate(activity.java:5231)             @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087)             @ android.app.activitythread.performlaunchactivity(activitythread.java:2148)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2233)             at android.app.activitythread.access$800(activitythread.java:135)             at android.app.activitythread$h.handlemessage(activitythread.java:1196)             at android.os.handler.dispatchmessage(handler.java:102)             at android.os.looper.loop(looper.java:136)             at android.app.activitythread.main(activitythread.java:5001)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:515)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:601)             at dalvik.system.nativestart.main(native method)* 

please help!!!

the problem context null.

however, activity class, service class extends context, should able replace context this or getapplicationcontext().

try this:

private int downloadsettings() {     pref = getapplicationcontext().getsharedpreferences("distance", activity.mode_private);     value = pref.getint("settings",15);       return value; } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -