android - Unable to Login after Registration -
i have created login page , registration page.after registering application unable utilize values login i.e; unable login credentials whatever have entered during registration.please help
my code
dbhelper.java
public class dbhelper extends sqliteopenhelper { private sqlitedatabase db; public static final string key_rowid = "_id"; public static final string key_fname = "firstname"; public static final string key_lname = "lastname"; public static final string key_gender = "gender"; public static final string key_user = "username"; public static final string key_email = "email"; dbhelper db = null; private static final string database_name = "srikanth1.db"; private static final int database_version = 2; public static final string database_table_name = "sri1"; private static final string database_table_create = "create table " + database_table_name + "(" + "_id integer primary key autoincrement,"+ "firstname text not null, lastname text not null, gender text not null, username text not null, password text not null, email text not null);"; public dbhelper(context context) { super(context, database_name, null, database_version); system.out.println("in constructor"); } @override public void oncreate(sqlitedatabase db) { try{ db.execsql(database_table_create); }catch(exception e){ e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } public cursor rawquery(string string, string[] strings) { // todo auto-generated method stub homecoming null; } public void open() { getwritabledatabase(); } public cursor getdetails(string text) throws sqlexception { cursor mcursor = db.query(true, database_table_name, new string[]{key_rowid, key_fname, key_lname, key_gender, key_user, key_email}, key_user + "=" + text, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } homecoming mcursor; } }
registration.java
public class registration extends activity implements onclicklistener, onitemselectedlistener { // variable declaration should in oncreate() private button msubmit; private button mcancel; private edittext mfname; private edittext mlname; private edittext musername; private edittext mpassword; private edittext memail; private spinner mgender; private string gen; protected dbhelper db = new dbhelper(registration.this); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.register); //assignment of ui fields variables msubmit = (button)findviewbyid(r.id.submit); msubmit.setonclicklistener(this); mcancel = (button)findviewbyid(r.id.cancel); mcancel.setonclicklistener(this); mfname = (edittext)findviewbyid(r.id.efname); mlname = (edittext)findviewbyid(r.id.elname); musername = (edittext)findviewbyid(r.id.reuname); mpassword = (edittext)findviewbyid(r.id.repass); memail = (edittext)findviewbyid(r.id.eemail); mgender = (spinner)findviewbyid(r.id.spinner1); // spinner method read on selected value arrayadapter<state> spinnerarrayadapter = new arrayadapter<state>(this, android.r.layout.simple_spinner_item, new state[] { new state("male"), new state("female")}); mgender.setadapter(spinnerarrayadapter); mgender.setonitemselectedlistener(this); } public void onclick(view v) { /* int id = v.getid(); if (id == r.id.cancel) { intent = new intent(getbasecontext(), loginactivity.class); startactivity(i); } else if (id == r.id.submit) { string fname = mfname.gettext().tostring(); string lname = mlname.gettext().tostring(); string uname = musername.gettext().tostring(); string pass = mpassword.gettext().tostring(); string email = memail.gettext().tostring(); boolean invalid = false; if(fname.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "enter firstname", toast.length_short).show(); } else if(lname.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in lastname", toast.length_short).show(); } else if(uname.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in username", toast.length_short).show(); } else if(pass.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in password", toast.length_short).show(); } else if(email.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in email id", toast.length_short).show(); } else if(invalid == false) { addentry(fname, lname, gen, uname, pass, email); intent i_register = new intent(registration.this, loginactivity.class); startactivity(i_register); //finish(); } }*/ switch(v.getid()){ case r.id.cancel: intent = new intent(getbasecontext(), loginactivity.class); startactivity(i); //finish(); break; case r.id.submit: string fname = mfname.gettext().tostring(); string lname = mlname.gettext().tostring(); string uname = musername.gettext().tostring(); string pass = mpassword.gettext().tostring(); string email = memail.gettext().tostring(); boolean invalid = false; if(fname.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "enter firstname", toast.length_short).show(); } else if(lname.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in lastname", toast.length_short).show(); } else if(uname.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in username", toast.length_short).show(); } else if(pass.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in password", toast.length_short).show(); } else if(email.equals("")) { invalid = true; toast.maketext(getapplicationcontext(), "please come in email id", toast.length_short).show(); } else if(invalid == false) { addentry(fname, lname, gen, uname, pass, email); intent i_register = new intent(registration.this, loginactivity.class); startactivity(i_register); //finish(); } break; } } public void ondestroy() { super.ondestroy(); db.close(); } private void addentry(string fname, string lname, string gen, string uname, string pass, string email) { sqlitedatabase db = db.getwritabledatabase(); contentvalues values = new contentvalues(); values.put("firstname", fname); values.put("lastname", lname); values.put("gender", gen); values.put("username", uname); values.put("password", pass); values.put("email", email); seek { db.insert(dbhelper.database_table_name, null, values); toast.maketext(getapplicationcontext(), "your details submitted successfully...", toast.length_short).show(); } catch(exception e) { e.printstacktrace(); } } public void onitemselected(adapterview<?> parent, view view, int position, long id) { // selected state object spinner state st = (state)mgender.getselecteditem(); // show via toast toaststate( "onitemselected", st ); } public void toaststate(string name, state st) { if ( st != null ) { gen = st.name; //toast.maketext(getbasecontext(), gen, toast.length_short).show(); } } public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }
loginactivity .java
public class loginactivity extends activity implements onclicklistener { button mlogin; button mregister; edittext muname; edittext mpassword; dbhelper db = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); mregister = (button)findviewbyid(r.id.register); mregister.setonclicklistener(this); mlogin = (button)findviewbyid(r.id.login); mlogin.setonclicklistener(this); } public void onclick(view v) { switch(v.getid()) { case r.id.register: intent = new intent(getbasecontext(), registration.class); startactivity(i); break; case r.id.login: muname = (edittext)findviewbyid(r.id.ledituname); mpassword = (edittext)findviewbyid(r.id.leditpw); string username = muname.gettext().tostring(); string password = mpassword.gettext().tostring(); if(username.equals("") || username == null) { toast.maketext(getapplicationcontext(), "please come in user name", toast.length_short).show(); } else if(password.equals("") || password == null) { toast.maketext(getapplicationcontext(), "please come in password", toast.length_short).show(); } else { boolean validlogin = validatelogin(username, password, getbasecontext()); if(validlogin) { //system.out.println("in valid"); intent in = new intent(getbasecontext(), tabbar.class); //in.putextra("username", muname.gettext().tostring()); startactivity(in); //finish(); } } break; } } private boolean validatelogin(string username, string password, context basecontext) { db = new dbhelper(getbasecontext()); sqlitedatabase db = db.getreadabledatabase(); string[] columns = {"_id"}; string selection = "username=? , password=?"; string[] selectionargs = {username,password}; cursor cursor = null; try{ cursor = db.query(dbhelper.database_table_name, columns, selection, selectionargs, null, null, null); startmanagingcursor(cursor); } catch(exception e) { e.printstacktrace(); } int numberofrows = cursor.getcount(); if(numberofrows <= 0) { toast.maketext(getapplicationcontext(), "user name , password miss match..\nplease seek again", toast.length_long).show(); intent intent = new intent(getbasecontext(), loginactivity.class); startactivity(intent); homecoming false; } homecoming true; } public void ondestroy() { super.ondestroy(); db.close(); } }
register.xml
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollview1" android:layout_width="match_parent" android:layout_height="wrap_content"> <linearlayout android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <textview android:id="@+id/fname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="first name" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/efname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:singleline="true"> <requestfocus /> </edittext> <textview android:id="@+id/lname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="last name" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/elname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:singleline="true"/> <textview android:id="@+id/rgender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="gender" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:drawselectorontop="true" /> <textview android:id="@+id/runame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="user name" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/reuname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp"/> <textview android:id="@+id/rpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/repass" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpassword" android:layout_marginleft="5dp" android:layout_marginright="5dp"/> <textview android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="email" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/eemail" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textemailaddress" android:layout_marginleft="5dp" android:layout_marginright="5dp"/> <relativelayout android:id="@+id/relativelayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="submit" android:layout_marginleft="5dp" android:layout_marginright="5dp"/> <button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="cancel" android:layout_torightof="@id/submit"/> </relativelayout> </linearlayout> </scrollview>
activity_login.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:id="@+id/luname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="user name" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/ledituname" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleline="true" android:layout_marginleft="5dp" android:layout_marginright="5dp"> <requestfocus /> </edittext> <textview android:id="@+id/lpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/leditpw" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpassword" android:layout_marginleft="5dp" android:layout_marginright="5dp"/> <relativelayout android:id="@+id/relativelayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:text="login" /> <button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="register" android:layout_torightof="@id/login"/> </relativelayout>
logcatfile
06-26 07:07:17.980: e/androidruntime(791): fatal exception: main 06-26 07:07:17.980: e/androidruntime(791): process: com.example.app, pid: 791 06-26 07:07:17.980: e/androidruntime(791): java.lang.nullpointerexception 06-26 07:07:17.980: e/androidruntime(791): @ com.example.app.loginactivity.validatelogin(loginactivity.java:109) 06-26 07:07:17.980: e/androidruntime(791): @ com.example.app.loginactivity.onclick(loginactivity.java:71) 06-26 07:07:17.980: e/androidruntime(791): @ android.view.view.performclick(view.java:4438) 06-26 07:07:17.980: e/androidruntime(791): @ android.view.view$performclick.run(view.java:18422) 06-26 07:07:17.980: e/androidruntime(791): @ android.os.handler.handlecallback(handler.java:733) 06-26 07:07:17.980: e/androidruntime(791): @ android.os.handler.dispatchmessage(handler.java:95) 06-26 07:07:17.980: e/androidruntime(791): @ android.os.looper.loop(looper.java:136) 06-26 07:07:17.980: e/androidruntime(791): @ android.app.activitythread.main(activitythread.java:5017) 06-26 07:07:17.980: e/androidruntime(791): @ java.lang.reflect.method.invokenative(native method) 06-26 07:07:17.980: e/androidruntime(791): @ java.lang.reflect.method.invoke(method.java:515) 06-26 07:07:17.980: e/androidruntime(791): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 06-26 07:07:17.980: e/androidruntime(791): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 06-26 07:07:17.980: e/androidruntime(791): @ dalvik.system.nativestart.main(native method)
replace this:
db = new dbhelper(getbasecontext());
to this,
db = new dbhelper(loginactivity.this);
android android-layout android-intent android-activity android-fragments
No comments:
Post a Comment