java - Android: Shared preferences not working -


i have following sharedpreferences set following:

public class mainactivity extends activity implements onclicklistener {      button openedlockers, closedlockers, checkout, admin;     sharedpreferences.editor editor = getpreferences(mode_private).edit(); 

i have sharedpreferences else:

 private void runonce() {         // todo auto-generated method stub         sharedpreferences settings = getsharedpreferences("prefs_name", 0);         mboolean = settings.getboolean("first_run", false);         if (!mboolean) {             // thing first time             settings = getsharedpreferences("prefs_name", 0);             sharedpreferences.editor editor = settings.edit();             editor.putboolean("first_run", true);             editor.commit();             prepopulate();         } else {             // other time app loads         }     } 

then call it:

public boolean onoptionsitemselected(menuitem item) {         // todo auto-generated method stub         switch (item.getitemid()) {         case r.id.setip:             alertdialog alert = new alertdialog.builder(mainactivity.this)                     .create();             alert.settitle("enter new ip");             alert.setmessage("enter new ip");             final edittext input = new edittext(this);             alert.setview(input);             alert.setbutton("submit", new dialoginterface.onclicklistener() {                  @override                 public void onclick(dialoginterface dialog, int which) {                     // todo auto-generated method stub                     string value = input.gettext().tostring();                     editor.putstring("ip", value);                     editor.commit();                     sharedpreferences preferences = getpreferences(mode_private);                     string tut = preferences.getstring("ip", "");                     ip = tut;                 }             });             break;         }         return false;     } 

however when run application following:

04-07 12:49:30.085:w/dalvikvm(12538): threadid=1: thread exiting uncaught exception (group=0x41679c08)  04-07 12:49:30.085: e/androidruntime(12538): fatal exception: main  04-07 12:49:30.085: e/androidruntime(12538): process: com.samsunglockercenter, pid: 12538  04-07 12:49:30.085: e/androidruntime(12538): java.lang.runtimeexception: unable instantiate activity componentinfo{com.samsunglockercenter/com.samsunglockercenter.mainactivity}: java.lang.nullpointerexception  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2224)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2356)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activitythread.access$800(activitythread.java:164)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activitythread$h.handlemessage(activitythread.java:1258)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.os.handler.dispatchmessage(handler.java:102)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.os.looper.loop(looper.java:157)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activitythread.main(activitythread.java:5341)  04-07 12:49:30.085: e/androidruntime(12538):    @ java.lang.reflect.method.invokenative(native method)  04-07 12:49:30.085: e/androidruntime(12538):    @ java.lang.reflect.method.invoke(method.java:515)  04-07 12:49:30.085: e/androidruntime(12538):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1259)    04-07 12:49:30.085: e/androidruntime(12538):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1075)  04-07 12:49:30.085: e/androidruntime(12538):    @ dalvik.system.nativestart.main(native method)  04-07 12:49:30.085: e/androidruntime(12538): caused by: java.lang.nullpointerexception  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activity.getlocalclassname(activity.java:4668)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activity.getpreferences(activity.java:4701)  04-07 12:49:30.085: e/androidruntime(12538):    @ com.samsunglockercenter.mainactivity.<init>(mainactivity.java:31)  04-07 12:49:30.085: e/androidruntime(12538):    @ java.lang.class.newinstanceimpl(native method)  04-07 12:49:30.085: e/androidruntime(12538):    @ java.lang.class.newinstance(class.java:1208)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.instrumentation.newactivity(instrumentation.java:1079)  04-07 12:49:30.085: e/androidruntime(12538):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2215)  04-07 12:49:30.085: e/androidruntime(12538):    ... 11 more 

any ideas?

move initialization

sharedpreferences.editor editor = getpreferences(mode_private).edit(); 

to oncreate().

you cannot use activity context when member variables initialized.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -