Android Java IllegalStateException: Could not execute method of the activity -
i'm trying code simple randomizer app. had randomizer button working, changed code (which thought irrelevant randomizer button) , started crashing , getting "illegalstateexception: not execute method of activity" error. can tell, error specific code is, because not find answers fit code.
package com.example.randomgamechooser; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.textview; public class mainscreen extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_screen); } public void choosegame (view view) { gamelist dbutil = new gamelist(this); dbutil.open(); string string = dbutil.getrandomentry(); //textview textview = new textview(this); textview textview = (textview) findviewbyid(r.id.chosenbox); textview.settextsize(40); textview.settext(string); //setcontentview (textview); dbutil.close(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main_screen, menu); return true; } //starts game selection activity public void opengames (view view) { intent intent = new intent(this, gameselction.class); startactivity(intent); } }
and referenced gamelist class
package com.example.randomgamechooser; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; import java.util.random; public class gamelist { private static final string tag = "gamelist"; //database name private static final string database_name = "game_list"; //database version private static final int database_version = 1; //table name private static final string database_table = "game_list"; //table columns public static final string key_name = "name"; public static final string key_genre = "genre"; public static final string key_rowid = "_id"; //database creation sql statement private static final string create_game_table = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_name +" text not null, " + key_genre + " text not null);"; //context private final context mctx; private databasehelper mdbhelper; private static sqlitedatabase mdb; //inner private class. database helper class creating , updating database. private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } // oncreate method called 1st time when database doesn't exists. @override public void oncreate(sqlitedatabase db) { log.i(tag, "creating database: " + create_game_table); db.execsql(create_game_table); } //onupgrade method called when database version changes. @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion); } } //constructor - takes context allow database opened/created //@param ctx context within work public gamelist(context ctx) { this.mctx = ctx; } //this method used creating/opening connection //@return instance of gamelist //@throws sqlexception public gamelist open() throws sqlexception { mdbhelper = new databasehelper(mctx); mdb = mdbhelper.getwritabledatabase(); return this; } //this method used closing connection. public void close() { mdbhelper.close(); } //this method used create/insert new game. //@param name // @param genre // @return long public long creategame(string name, string genre) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_name, name); initialvalues.put(key_genre, genre); return mdb.insert(database_table, null, initialvalues); } // method delete game. // @param rowid // @return boolean public static boolean deletegame(long rowid) { return mdb.delete(database_table, key_rowid + "=" + rowid, null) > 0; } // method return cursor holding games. // @return cursor public cursor fetchallgames() { return mdb.query(database_table, new string[] {key_rowid, key_name, key_genre}, null, null, null, null, null); } // method return cursor holding specific game. // @param id // @return cursor // @throws sqlexception public cursor fetchgame(long id) throws sqlexception { cursor mcursor = mdb.query(true, database_table, new string[] {key_rowid, key_name, key_genre}, key_rowid + "=" + id, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } public int getallentries() { cursor cursor = mdb.rawquery( "select count(name) game_list", null); if(cursor.movetofirst()) { return cursor.getint(0); } return cursor.getint(0); } public string getrandomentry() { //id = getallentries(); random random = new random(); int rand = random.nextint(getallentries()); if(rand == 0) ++rand; cursor cursor = mdb.rawquery( "select name game_list _id = " + rand, null); if(cursor.movetofirst()) { return cursor.getstring(0); } return cursor.getstring(0); } // method update game. // @param id // @param name // @param standard // @return boolean public boolean updategame(int id, string name, string standard) { contentvalues args = new contentvalues(); args.put(key_name, name); args.put(key_genre, standard); return mdb.update(database_table, args, key_rowid + "=" + id, null) > 0; } }
and here error log
07-31 14:50:45.215: d/androidruntime(280): shutting down vm 07-31 14:50:45.215: w/dalvikvm(280): threadid=1: thread exiting uncaught exception (group=0x4001d800) 07-31 14:50:45.236: e/androidruntime(280): fatal exception: main 07-31 14:50:45.236: e/androidruntime(280): java.lang.illegalstateexception: not execute method of activity 07-31 14:50:45.236: e/androidruntime(280): @ android.view.view$1.onclick(view.java:2072) 07-31 14:50:45.236: e/androidruntime(280): @ android.view.view.performclick(view.java:2408) 07-31 14:50:45.236: e/androidruntime(280): @ android.view.view$performclick.run(view.java:8816) 07-31 14:50:45.236: e/androidruntime(280): @ android.os.handler.handlecallback(handler.java:587) 07-31 14:50:45.236: e/androidruntime(280): @ android.os.handler.dispatchmessage(handler.java:92) 07-31 14:50:45.236: e/androidruntime(280): @ android.os.looper.loop(looper.java:123) 07-31 14:50:45.236: e/androidruntime(280): @ android.app.activitythread.main(activitythread.java:4627) 07-31 14:50:45.236: e/androidruntime(280): @ java.lang.reflect.method.invokenative(native method) 07-31 14:50:45.236: e/androidruntime(280): @ java.lang.reflect.method.invoke(method.java:521) 07-31 14:50:45.236: e/androidruntime(280): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 07-31 14:50:45.236: e/androidruntime(280): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 07-31 14:50:45.236: e/androidruntime(280): @ dalvik.system.nativestart.main(native method) 07-31 14:50:45.236: e/androidruntime(280): caused by: java.lang.reflect.invocationtargetexception 07-31 14:50:45.236: e/androidruntime(280): @ com.example.randomgamechooser.mainscreen.choosegame(mainscreen.java:23) 07-31 14:50:45.236: e/androidruntime(280): @ java.lang.reflect.method.invokenative(native method) 07-31 14:50:45.236: e/androidruntime(280): @ java.lang.reflect.method.invoke(method.java:521) 07-31 14:50:45.236: e/androidruntime(280): @ android.view.view$1.onclick(view.java:2067) 07-31 14:50:45.236: e/androidruntime(280): ... 11 more 07-31 14:50:45.236: e/androidruntime(280): caused by: android.database.cursorindexoutofboundsexception: index 0 requested, size of 0 07-31 14:50:45.236: e/androidruntime(280): @ android.database.abstractcursor.checkposition(abstractcursor.java:580) 07-31 14:50:45.236: e/androidruntime(280): @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:214) 07-31 14:50:45.236: e/androidruntime(280): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:41) 07-31 14:50:45.236: e/androidruntime(280): @ com.example.randomgamechooser.gamelist.getrandomentry(gamelist.java:153) 07-31 14:50:45.236: e/androidruntime(280): ... 15 more
read stacktrace carefuly. answer @ last "caused by" exception;
07-31 14:50:45.236: e/androidruntime(280): caused by: android.database.cursorindexoutofboundsexception: index 0 requested, size of 0 07-31 14:50:45.236: e/androidruntime(280): @ android.database.abstractcursor.checkposition(abstractcursor.java:580) 07-31 14:50:45.236: e/androidruntime(280): @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:214) 07-31 14:50:45.236: e/androidruntime(280): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:41) 07-31 14:50:45.236: e/androidruntime(280): @ com.example.randomgamechooser.gamelist.getrandomentry(gamelist.java:153) 07-31 14:50:45.236: e/androidruntime(280): ... 15 more
query in method getrandomentry() returns empty result, while read first position.
Comments
Post a Comment