android - SharedPreferences retreival error -



android - SharedPreferences retreival error -

well after researching thorough.. i've got code it. resources ive seen show code syntax right i'm getting forcefulness quit issues app.. here current code:

package com.monatecsolutions.safen_secure; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.preference.preferencemanager; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; /** * created cchance on 6/24/2014. */ public class settingactivity extends activity { textview txt_nameval; edittext txt_delayval; button btn_save; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_settings); // grab preferences final sharedpreferences preferences = getsharedpreferences("myprefs", 0); // made final onclicklistener txt_nameval = (textview) findviewbyid(r.id.txt_myname); txt_delayval = (edittext) findviewbyid(r.id.txt_delay); btn_save = (button) findviewbyid(r.id.btnsave); // setting text preferences string myname = preferences.getstring("myname", ""); int mydelay = preferences.getint("delay", 60); txt_nameval.settext(myname); txt_delayval.settext(mydelay); btn_save.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // saving settings , returning main screen preferences.edit().putstring("myname", txt_nameval.gettext().tostring()).commit(); preferences.edit().putint("delay", integer.parseint(txt_delayval.gettext().tostring())).commit(); startactivity(new intent(settingactivity.this, myactivity.class)); } }); } }

from logcat im getting this:

06-24 15:12:50.679 1852-1852/com.monatecsolutions.safen_secure e/androidruntime﹕ fatal exception: main process: com.monatecsolutions.safen_secure, pid: 1852 java.lang.runtimeexception: unable instantiate activity componentinfo{com.monatecsolutions.safen_secure/com.monatecsolutions.safen_secure.settingactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2121) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ 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:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ android.content.contextwrapper.getsharedpreferences(contextwrapper.java:173) @ com.monatecsolutions.safen_secure.settingactivity.<init>(settingactivity.java:21) @ java.lang.class.newinstanceimpl(native method) @ java.lang.class.newinstance(class.java:1208) @ android.app.instrumentation.newactivity(instrumentation.java:1061) @ android.app.activitythread.performlaunchactivity(activitythread.java:2112)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2245)             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:5017)             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:779)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:595)             at dalvik.system.nativestart.main(native method)

i hope can shed lite on me. come c# background may doing know can in c# not realizing cant done in java.

move sharedpreferences creation code within oncreate:

@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_settings); preferences = getsharedpreferences("myprefs", 0); //other stuffs here }

[edit1] replace integer int in next statement:

integer mydelay = preferences.getint("delay", 60);

[edit2] did register activity in manifest file?

[edit3] culprit statement:

txt_delayval.settext(mydelay);

replace with:

txt_delayval.settext("" + mydelay);

android sharedpreferences

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -