sqlite - Android - java.lang.IndexOutOfBoundsException: Invalid Index 1, size is 1 -
i'm creating quiz app , import sqlite database file android project, looks :

it has id, actual question, when right answer, 3 wrong answers , difficulty of question. id has number, question , answers has text type, because want add numbers , text in fields, , difficulty has number type. import file assets folder named dbquestion , tried run app, pressed play button, , opens first question database, when check radio button , press next question these errors:
08-08 06:42:15.266: d/androidruntime(2459): shutting down vm 08-08 06:42:15.266: w/dalvikvm(2459): threadid=1: thread exiting uncaught exception (group=0x414c4700) 08-08 06:42:15.367: e/androidruntime(2459): fatal exception: main 08-08 06:42:15.367: e/androidruntime(2459): java.lang.runtimeexception: unable start activity componentinfo{com.tmm.android.chuck/com.tmm.android.chuck.questionactivity}: java.lang.indexoutofboundsexception: invalid index 1, size 1 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activitythread.access$600(activitythread.java:141) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 08-08 06:42:15.367: e/androidruntime(2459): @ android.os.handler.dispatchmessage(handler.java:99) 08-08 06:42:15.367: e/androidruntime(2459): @ android.os.looper.loop(looper.java:137) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activitythread.main(activitythread.java:5103) 08-08 06:42:15.367: e/androidruntime(2459): @ java.lang.reflect.method.invokenative(native method) 08-08 06:42:15.367: e/androidruntime(2459): @ java.lang.reflect.method.invoke(method.java:525) 08-08 06:42:15.367: e/androidruntime(2459): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 08-08 06:42:15.367: e/androidruntime(2459): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-08 06:42:15.367: e/androidruntime(2459): @ dalvik.system.nativestart.main(native method) 08-08 06:42:15.367: e/androidruntime(2459): caused by: java.lang.indexoutofboundsexception: invalid index 1, size 1 08-08 06:42:15.367: e/androidruntime(2459): @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) 08-08 06:42:15.367: e/androidruntime(2459): @ java.util.arraylist.get(arraylist.java:308) 08-08 06:42:15.367: e/androidruntime(2459): @ com.tmm.android.chuck.quiz.gameplay.getnextquestion(gameplay.java:112) 08-08 06:42:15.367: e/androidruntime(2459): @ com.tmm.android.chuck.questionactivity.oncreate(questionactivity.java:39) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activity.performcreate(activity.java:5133) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-08 06:42:15.367: e/androidruntime(2459): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 08-08 06:42:15.367: e/androidruntime(2459): ... 11 more 08-08 06:42:19.576: d/dalvikvm(2485): gc_for_alloc freed 67k, 6% free 2844k/3016k, paused 187ms, total 197ms 08-08 06:42:20.756: d/gralloc_goldfish(2485): emulator without gpu emulation detected. this how dbhelper.class looks like:
package com.tmm.android.chuck.db; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.arraylist; import java.util.list; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import com.tmm.android.chuck.quiz.question; /** * @author robert.hinds * */ public class dbhelper extends sqliteopenhelper { // android's default system path of application database private static string db_path = "/data/data/com.tmm.android.chuck/databases/"; private static string db_name = "dbquestion"; private sqlitedatabase mydatabase; private final context mycontext; /** * constructor takes , keeps reference of passed context in order * access application assets , resources. * * @param context */ public dbhelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on system , rewrites own * database. * */ public void createdatabase() throws ioexception { boolean dbexist = checkdatabase(); if (!dbexist) { // calling method , empty database created // default system path // of application gonna able overwrite // database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each * time open application. * * @return true if exists, false if doesn't */ private boolean checkdatabase() { file dbfile = mycontext.getdatabasepath(db_name); return dbfile.exists(); } /** * copies database local assets-folder created * empty database in system folder, can accessed , * handled. done transfering bytestream. * */ private void copydatabase() throws ioexception { // open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; // open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); // transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception { // open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } @override public synchronized void close() { if (mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } // add public helper methods access , content // database. // return cursors doing "return mydatabase.query(....)" it'd // easy // create adapters views. public list<question> getquestionset(int difficulty, int numq) { list<question> questionset = new arraylist<question>(); cursor c = mydatabase.rawquery( "select * questions difficulty=" + difficulty + " order random() limit " + numq, null); while (c.movetonext()) { // log.d("question", "question found in db: " + c.getstring(1)); question q = new question(); q.setquestion(c.getstring(1)); q.setanswer(c.getstring(2)); q.setoption1(c.getstring(3)); q.setoption2(c.getstring(4)); q.setoption3(c.getstring(5)); q.setrating(difficulty); questionset.add(q); } return questionset; } } gameplay.class :
/** * */ package com.tmm.android.chuck.quiz; import java.util.arraylist; import java.util.list; /** * @author robert.hinds * * class represents current game being played * tracks score , player details * */ public class gameplay { private int numrounds; private int difficulty; private string playername; private int right; private int wrong; private int round; private list<question> questions = new arraylist<question>(); /** * @return playername */ public string getplayername() { return playername; } /** * @param playername playername set */ public void setplayername(string playername) { this.playername = playername; } /** * @return right */ public int getright() { return right; } /** * @param right right set */ public void setright(int right) { this.right = right; } /** * @return wrong */ public int getwrong() { return wrong; } /** * @param wrong wrong set */ public void setwrong(int wrong) { this.wrong = wrong; } /** * @return round */ public int getround() { return round; } /** * @param round round set */ public void setround(int round) { this.round = round; } /** * @param difficulty difficulty set */ public void setdifficulty(int difficulty) { this.difficulty = difficulty; } /** * @return difficulty */ public int getdifficulty() { return difficulty; } /** * @param questions questions set */ public void setquestions(list<question> questions) { this.questions = questions; } /** * @param q question add */ public void addquestions(question q) { this.questions.add(q); } /** * @return questions */ public list<question> getquestions() { return questions; } public question getnextquestion(){ //get question question next = questions.get(this.getround()); //update round number next round this.setround(this.getround()+1); return next; } /** * method increment number of correct answers game */ public void incrementrightanswers(){ right ++; } /** * method increment number of incorrect answers game */ public void incrementwronganswers(){ wrong ++; } /** * @param numrounds numrounds set */ public void setnumrounds(int numrounds) { this.numrounds = numrounds; } /** * @return numrounds */ public int getnumrounds() { return numrounds; } /** * method checks if game on * @return boolean */ public boolean isgameover(){ return (getround() >= getnumrounds()); } } appreciate :)
this whole project ->> https://www.dropbox.com/s/e7jl14b1503aj6v/androidchuckquiz-master.rar
try this.questions =new arraylist<question>(questions);
also try
c.movetonext(); while (!c.isafterlast()) { // log.d("question", "question found in db: " + c.getstring(1)); question q = new question(); q.setquestion(c.getstring(1)); q.setanswer(c.getstring(2)); q.setoption1(c.getstring(3)); q.setoption2(c.getstring(4)); q.setoption3(c.getstring(5)); q.setrating(difficulty); questionset.add(q); c.movetonext(); }
Comments
Post a Comment