android - Couldn't read row from CursorWindow -


i'm doing android application in want implement registration feature new user added.

at first have created activity called registration, , when button pressed calls method add user:

public user createuser(string username, string password, string email) {             contentvalues values = new contentvalues();             values.put(mysqlitehelperusers.column_username, username);             values.put(mysqlitehelperusers.column_password, password);             values.put(mysqlitehelperusers.column_email, email);             long insertid = database.insert(mysqlitehelperusers.table_users, null,                 values);             cursor cursor = database.query(mysqlitehelperusers.table_users,                 allcolumns, mysqlitehelperusers.column_id + " = " + insertid, null,                 null, null, null);             cursor.movetofirst();             user newuser = cursortouser(cursor);             cursor.close();             return newuser; } 

and here cursortouser method:

private user cursortouser(cursor cursor) {         user user = new user();         user.setid(cursor.getlong(cursor.getcolumnindex("_id")));         user.setusername(cursor.getstring(cursor.getcolumnindex("_username")));         user.setpassword(cursor.getstring(cursor.getcolumnindex("_password")));         user.setemail(cursor.getstring(cursor.getcolumnindex("_email")));         return user; } 

but gives me errors:

    05-10 13:09:35.081: e/androidruntime(16380): fatal exception: main 05-10 13:09:35.081: e/androidruntime(16380): java.lang.illegalstateexception: not execute method of activity 05-10 13:09:35.081: e/androidruntime(16380):    @ android.view.view$1.onclick(view.java:3704) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.view.view.performclick(view.java:4232) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.view.view$performclick.run(view.java:17298) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.os.handler.handlecallback(handler.java:615) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.os.handler.dispatchmessage(handler.java:92) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.os.looper.loop(looper.java:137) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.app.activitythread.main(activitythread.java:4921) 05-10 13:09:35.081: e/androidruntime(16380):    @ java.lang.reflect.method.invokenative(native method) 05-10 13:09:35.081: e/androidruntime(16380):    @ java.lang.reflect.method.invoke(method.java:511) 05-10 13:09:35.081: e/androidruntime(16380):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 05-10 13:09:35.081: e/androidruntime(16380):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 05-10 13:09:35.081: e/androidruntime(16380):    @ dalvik.system.nativestart.main(native method) 05-10 13:09:35.081: e/androidruntime(16380): caused by: java.lang.reflect.invocationtargetexception 05-10 13:09:35.081: e/androidruntime(16380):    @ java.lang.reflect.method.invokenative(native method) 05-10 13:09:35.081: e/androidruntime(16380):    @ java.lang.reflect.method.invoke(method.java:511) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.view.view$1.onclick(view.java:3699) 05-10 13:09:35.081: e/androidruntime(16380):    ... 11 more 05-10 13:09:35.081: e/androidruntime(16380): caused by: java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow.  make sure cursor initialized correctly before accessing data it. 05-10 13:09:35.081: e/androidruntime(16380):    @ android.database.cursorwindow.nativegetlong(native method) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.database.cursorwindow.getlong(cursorwindow.java:511) 05-10 13:09:35.081: e/androidruntime(16380):    @ android.database.abstractwindowedcursor.getlong(abstractwindowedcursor.java:75) 05-10 13:09:35.081: e/androidruntime(16380):    @ dao.usersdatasource.cursortouser(usersdatasource.java:75) 05-10 13:09:35.081: e/androidruntime(16380):    @ dao.usersdatasource.createuser(usersdatasource.java:44) 05-10 13:09:35.081: e/androidruntime(16380):    @ com.example.pocket_city.registration.registrationclick(registration.java:47) 05-10 13:09:35.081: e/androidruntime(16380):    ... 14 more 05-10 13:09:35.141: d/dalvikvm(16380): gc_concurrent freed 177k, 14% free 9641k/11143k, paused 15ms+4ms, total 79ms 

here mysqlitehelperusers class:

public class mysqlitehelperusers extends sqliteopenhelper{     public static final string table_users = "users";     public static final string column_id = "_id";     public static final string column_username = "_username";     public static final string column_password = "_password";     public static final string column_email = "_email";      private static final string database_name = "users.db";     private static final int database_version = 1;      // database creation sql statement     private static final string database_create = "create table "             + table_users.trim() + "("              + column_id.trim() + " integer primary key autoincrement, "              + column_username.trim() + " text not null, "             + column_password.trim() + " text not null, "             + column_email.trim()               + " text not null)";      public mysqlitehelperusers(context context) {         super(context, database_name, null, database_version);     }      @override     public void oncreate(sqlitedatabase database) {         database.execsql(database_create);     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists " + table_users);         oncreate(db);     } 

registration activity:

    public class registration extends activity {         usersdatasource usersdatasource = null;         user user = null;          @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.registration);               usersdatasource = new usersdatasource(getapplicationcontext());             usersdatasource.open();         }           public void registrationclick(view view){             edittext editusername = (edittext) findviewbyid(r.id.editusername);                 string username = editusername.gettext().tostring();             edittext editpassw = (edittext) findviewbyid(r.id.editpassw);                 string passw = editpassw.gettext().tostring();             edittext editpassw2 = (edittext) findviewbyid(r.id.editpassw2);                 string passw2 = editpassw2.gettext().tostring();             edittext editemail = (edittext) findviewbyid(r.id.editemail);                 string email = editemail.gettext().tostring();             edittext editemail2 = (edittext) findviewbyid(r.id.editemail2);                 string email2 = editemail2.gettext().tostring();             system.out.println("passw= "+passw+" "+passw2);             system.out.println("email= "+email+" "+email2);              /*if(!passw.equals(passw2) && !email.equals(email2)){                 toast.maketext(getapplicationcontext(), "le password/email non sono uguali!", toast.length_long).show();             }*/              usersdatasource.createuser(username, passw, email);             /*toast.maketext(getapplicationcontext(), "registrazione riuscita!", toast.length_long).show();*/                  }     } 

it seems 1 or more of columns you're trying missing in cursor.

check when create allcolumns includes columns:

string[] allcolumns = {"_id", "_username", "_password", "_email"}; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -