android - Getting NullPointerException at SharedPreferences -


i getting nullpointerexception @ sharedpreferences. here code:

public void onclick(view v) {             phone = (edittext) findviewbyid(r.id.phoneno);             final string number = phone.gettext().tostring();             new alertdialog.builder(mainactivity.this)                     .setmessage("confirm number:+91-" + number)                     .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int which) {                             sharedpreferences.editor editor = sharedpreferences.edit();                             editor.putstring(phone, number);                             editor.commit();                              intent intent = new intent(mainactivity.this, navigationfarmer.class);                             startactivity(intent);                             finish();                         }                     })                     .setnegativebutton(android.r.string.no, new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int which) {                             // nothing                         }                     })                     .seticon(android.r.drawable.ic_dialog_alert)                     .show();         } 

logcat:

java.lang.nullpointerexception
@ wolverine.example.com.btp_farmer.mainactivity$1$2.onclick(mainactivity.java:43)
at com.android.internal.app.alertcontroller$buttonhandler.handlemessage (alertcontroller.java:166)
at android.os.handler.dispatchmessage(handler.java:99)
at android.os.looper.loop(looper.java:137) (mainactivity.java:43):sharedpreferences.editor editor = sharedpreferences.edit();

try this:

 sharedpreferences.editor editor = getsharedpreferences(name,     context.mode_private).edit();  editor.putstring("name", "abc");  editor.commit(); 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -