android - Using SharedPreferences to store user password -
android - Using SharedPreferences to store user password -
so need simple app have password. default password "admin" , user can alter if he/she wants to. having problem on changing part. don't know why activity made forcefulness closes. it's first time utilize sharedpreferences. don't know if i've used correctly. please, help me. here's code:
import android.annotation.suppresslint; import android.app.activity; import android.content.sharedpreferences; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class changepass extends activity { @suppresslint("commitprefedits") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.change_pass); final sharedpreferences settings = changepass.this.getsharedpreferences("pass", 0); final sharedpreferences.editor editor = settings.edit(); final edittext curpass; final edittext newpass1; final edittext newpass2; button btnsubmit; final textview errormsg; curpass = (edittext)this.findviewbyid(r.id.curpw); newpass1 = (edittext)this.findviewbyid(r.id.newpw1); newpass2 = (edittext)this.findviewbyid(r.id.newpw2); btnsubmit = (button)this.findviewbyid(r.id.btnsubmit); errormsg = (button)this.findviewbyid(r.id.errortxt); btnsubmit.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub string current = curpass.gettext().tostring().trim(); string new1 = newpass1.gettext().tostring().trim(); string new2 = newpass2.gettext().tostring().trim(); string currentpw = settings.getstring("password", "admin"); if (currentpw.equals(current)) { if (new1.equals(new2)) { editor.putstring("password", new1).apply(); toast.maketext(getapplicationcontext(), "password changed!", toast.length_long).show(); curpass.settext(""); newpass1.settext(""); newpass2.settext(""); } else { errormsg.settext("error: passwords did not match."); } } else { errormsg.settext("error: wrong password."); } } }); }
xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/new_bg_pass" > <edittext android:id="@+id/curpw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="144dp" android:ems="10" android:inputtype="textpassword" > <requestfocus /> </edittext> <edittext android:id="@+id/newpw1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/curpw" android:layout_centerhorizontal="true" android:layout_margintop="57dp" android:ems="10" android:inputtype="textpassword" /> <textview android:id="@+id/textview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/newpw1" android:layout_alignleft="@+id/textview1" android:text="new password:" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/newpw2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/newpw1" android:layout_below="@+id/newpw1" android:layout_margintop="35dp" android:ems="10" android:inputtype="textpassword" /> <textview android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/newpw2" android:layout_alignleft="@+id/textview01" android:text="retype new password:" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/newpw1" android:layout_marginright="57dp" android:layout_toleftof="@+id/curpw" android:text="current password:" android:textappearance="?android:attr/textappearancemedium" /> <button android:id="@+id/btnsubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/newpw2" android:layout_centerhorizontal="true" android:layout_margintop="40dp" android:text="submit" /> <textview android:id="@+id/errortxt" android:layout_width="400dp" android:layout_height="wrap_content" android:layout_above="@+id/textview1" android:layout_centerhorizontal="true" android:layout_marginbottom="34dp" android:text="" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#ff0000" /> </relativelayout>
here's logcat:
02-20 12:28:58.156: e/androidruntime(338): fatal exception: main 02-20 12:28:58.156: e/androidruntime(338): java.lang.runtimeexception: unable start activity componentinfo{com.login/com.login.changepass}: java.lang.classcastexception: android.widget.textview 02-20 12:28:58.156: e/androidruntime(338): @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 02-20 12:28:58.156: e/androidruntime(338): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 02-20 12:28:58.156: e/androidruntime(338): @ android.app.activitythread.access$1500(activitythread.java:117) 02-20 12:28:58.156: e/androidruntime(338): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 02-20 12:28:58.156: e/androidruntime(338): @ android.os.handler.dispatchmessage(handler.java:99) 02-20 12:28:58.156: e/androidruntime(338): @ android.os.looper.loop(looper.java:123) 02-20 12:28:58.156: e/androidruntime(338): @ android.app.activitythread.main(activitythread.java:3683) 02-20 12:28:58.156: e/androidruntime(338): @ java.lang.reflect.method.invokenative(native method) 02-20 12:28:58.156: e/androidruntime(338): @ java.lang.reflect.method.invoke(method.java:507) 02-20 12:28:58.156: e/androidruntime(338): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 02-20 12:28:58.156: e/androidruntime(338): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 02-20 12:28:58.156: e/androidruntime(338): @ dalvik.system.nativestart.main(native method) 02-20 12:28:58.156: e/androidruntime(338): caused by: java.lang.classcastexception: android.widget.textview 02-20 12:28:58.156: e/androidruntime(338): @ com.login.changepass.oncreate(changepass.java:35) 02-20 12:28:58.156: e/androidruntime(338): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 02-20 12:28:58.156: e/androidruntime(338): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 02-20 12:28:58.156: e/androidruntime(338): ... 11 more
you have defined textview
final textview errormsg;
and trying cast button:
errormsg = (button)this.findviewbyid(r.id.errortxt);
besides should cast below:
errormsg = (textview)this.findviewbyid(r.id.errortxt);
edited:
create share preference:
sharedpreferences sp=getsharedpreferences("login", 0); sharedpreferences.editor ed=sp.edit(); ed.putstring("psw",value); ed.commit();
get value share preference:
sharedpreferences sp1=this.getsharedpreferences("login",null); string pass = sp1.getstring("psw", null);
update share preference values:
if (pass.equals(current)) { ed.putstring("psw", new1); ed.commit(); }
android passwords sharedpreferences
Comments
Post a Comment