database - Android:getting illegalstateexception in my new emulator -
in old emulator program runs without problem.so thought program fine.but when try new emulator/or other emulators runs.the problem starts when click search button.when click,program unexpectedly closed.logcat says illegalstateexception , caused by: no such column as_id.but in database _id column exists.i can search in old emulator without exception.same code same database.why happening?why runs in 1 emulator? 1 please kindly tell how solve exception.
this java class program breaks
` public class employeelist extends listactivity { protected edittext searchtext; protected sqlitedatabase db; protected cursor cursor; protected listadapter adapter; public integer pid=null; public databasehelper databasehelper; public book book; private static final int delete_id = menu.first+3; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); db = (new databasehelper(this)).getwritabledatabase(); searchtext = (edittext) findviewbyid (r.id.searchtext); } public void search(view view) { //when click here exception // || concatenation operation in sqlite toast.maketext(getapplicationcontext(), "insert successfull", toast.length_long).show(); cursor = db.rawquery("select _id, firstname, lastname, title employee firstname || ' ' || lastname ?", new string[]{"%" + searchtext.gettext().tostring() + "%"}); adapter = new simplecursoradapter( this, r.layout.employee_list_item, cursor, new string[] {"firstname", "lastname", "title"}, new int[] {r.id.firstname, r.id.lastname, r.id.title}); setlistadapter(adapter); registerforcontextmenu(getlistview()); } public void onlistitemclick(listview parent, view view, int position, long id) { intent intent = new intent(this, employeedetails.class); cursor cursor = (cursor) adapter.getitem(position); intent.putextra("employee_id", cursor.getint(cursor.getcolumnindex("_id"))); startactivity(intent); } public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { menu.add(menu.none, delete_id, menu.none, "delete") .setalphabeticshortcut('d'); } @override public boolean oncontextitemselected(menuitem item) { switch (item.getitemid()) { case delete_id: adapterview.adaptercontextmenuinfo info= (adapterview.adaptercontextmenuinfo)item.getmenuinfo(); delete(info.id); return(true); } return(super.onoptionsitemselected(item)); } private void delete(final long rowid) { if (rowid>0) { new alertdialog.builder(this) .settitle(r.string.delete_title) .setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { processdelete(rowid); } }) .setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { // ignore, dismiss } }) .show(); } } private void processdelete(long rowid) { string[] args={string.valueof(rowid)}; databasehelper.getwritabledatabase().delete("employee", "_id=?", args); cursor.requery(); } } `
my database class:
`public class databasehelper extends sqliteopenhelper{ public static final string db_name = "employee_directory"; public static final integer version=1; public static final string table_name= "employee"; public static final string _id= "id"; public static final string firstname= "firstname"; public static final string lastname= "lastname"; public static final string title= "title"; public static final string officephone= "officephone"; public static final string cellphone= "cellphone"; public static final string email= "email"; public static final string table_sql = "create table " + table_name+" (" +_id+" integer primary key autoincrement, " + firstname+ " text, " + lastname+ " text, " + title+ " text, " + officephone+ " text, " + cellphone+" text, " + email+" text)"; public databasehelper(context context) { super(context, db_name, null, version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub log.d("table sql", table_sql); db.execsql(table_sql); } @override public void onupgrade(sqlitedatabase db, int oldv, int newv) { db.execsql("drop table if exists employees"); oncreate(db); } }`
my logcat says:
` 08-10 01:23:12.655: e/androidruntime(337): fatal exception: main 08-10 01:23:12.655: e/androidruntime(337): java.lang.illegalstateexception: not execute method of activity 08-10 01:23:12.655: e/androidruntime(337): @ android.view.view$1.onclick(view.java:2072) 08-10 01:23:12.655: e/androidruntime(337): @ android.view.view.performclick(view.java:2408) 08-10 01:23:12.655: e/androidruntime(337): @ android.view.view$performclick.run(view.java:8816) 08-10 01:23:12.655: e/androidruntime(337): @ android.os.handler.handlecallback(handler.java:587) 08-10 01:23:12.655: e/androidruntime(337): @ android.os.handler.dispatchmessage(handler.java:92) 08-10 01:23:12.655: e/androidruntime(337): @ android.os.looper.loop(looper.java:123) 08-10 01:23:12.655: e/androidruntime(337): @ android.app.activitythread.main(activitythread.java:4627) 08-10 01:23:12.655: e/androidruntime(337): @ java.lang.reflect.method.invokenative(native method) 08-10 01:23:12.655: e/androidruntime(337): @ java.lang.reflect.method.invoke(method.java:521) 08-10 01:23:12.655: e/androidruntime(337): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 08-10 01:23:12.655: e/androidruntime(337): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 08-10 01:23:12.655: e/androidruntime(337): @ dalvik.system.nativestart.main(native method) 08-10 01:23:12.655: e/androidruntime(337): caused by: java.lang.reflect.invocationtargetexception 08-10 01:23:12.655: e/androidruntime(337): @ com.example.again.employeelist.search(employeelist.java:46) 08-10 01:23:12.655: e/androidruntime(337): @ java.lang.reflect.method.invokenative(native method) 08-10 01:23:12.655: e/androidruntime(337): @ java.lang.reflect.method.invoke(method.java:521) 08-10 01:23:12.655: e/androidruntime(337): @ android.view.view$1.onclick(view.java:2067) 08-10 01:23:12.655: e/androidruntime(337): ... 11 more 08-10 01:23:12.655: e/androidruntime(337): caused by: android.database.sqlite.sqliteexception: no such column: _id: , while compiling: select _id, firstname, lastname, title employee firstname || ' ' || lastname ? 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitecompiledsql.native_compile(native method) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitecompiledsql.compile(sqlitecompiledsql.java:91) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitecompiledsql.<init>(sqlitecompiledsql.java:64) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:80) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:46) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:42) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1345) 08-10 01:23:12.655: e/androidruntime(337): @ android.database.sqlite.sqlitedatabase.rawquery(sqlitedatabase.java:1315) 08-10 01:23:12.655: e/androidruntime(337): ... 15 more `
may on first emulator table not updated !
and when run code(which contains bug) on emulator gives error
you using _id column in query while column name id in table
so try by
either
changing public static final string _id= "id";
to
public static final string _id= "_id";
inside databse class
or
change to
cursor = db.rawquery("select id, firstname, lastname, title employee firstname || ' ' || lastname ?", new string[]{"%" + searchtext.gettext().tostring() + "%"});
inside main class
Comments
Post a Comment