android - ArrayAdapter throwing ArrayIndexOutOfBoundsException -
i getting arrayoutofbounds exception when using custom array adapter.
i wondering if there coding errors have overlooked.
here error log:
06-10 20:21:53.254: e/androidruntime(315): fatal exception: main 06-10 20:21:53.254: e/androidruntime(315): java.lang.runtimeexception: unable start activity componentinfo{alex.android.galaxy.tab.latest/alex.android.galaxy.tab.latest.basic_db_output}: java.lang.arrayindexoutofboundsexception 06-10 20:21:53.254: e/androidruntime(315): @ android.app.activitythread.performlaunchactivity(activitythread.java:2663) 06-10 20:21:53.254: e/androidruntime(315): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2679) 06-10 20:21:53.254: e/androidruntime(315): @ android.app.activitythread.access$2300(activitythread.java:125) 06-10 20:21:53.254: e/androidruntime(315): @ android.app.activitythread$h.handlemessage(activitythread.java:2033) 06-10 20:21:53.254: e/androidruntime(315): @ android.os.handler.dispatchmessage(handler.java:99) 06-10 20:21:53.254: e/androidruntime(315): @ android.os.looper.loop(looper.java:123) 06-10 20:21:53.254: e/androidruntime(315): @ android.app.activitythread.main(activitythread.java:4627) 06-10 20:21:53.254: e/androidruntime(315): @ java.lang.reflect.method.invokenative(native method) 06-10 20:21:53.254: e/androidruntime(315): @ java.lang.reflect.method.invoke(method.java:521) 06-10 20:21:53.254: e/androidruntime(315): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 06-10 20:21:53.254: e/androidruntime(315): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 06-10 20:21:53.254: e/androidruntime(315): @ dalvik.system.nativestart.main(native method) 06-10 20:21:53.254: e/androidruntime(315): caused by: java.lang.arrayindexoutofboundsexception 06-10 20:21:53.254: e/androidruntime(315): @ alex.android.galaxy.tab.latest.basic_db_output.oncreate(basic_db_output.java:44) 06-10 20:21:53.254: e/androidruntime(315): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 06-10 20:21:53.254: e/androidruntime(315): @ android.app.activitythread.performlaunchactivity(activitythread.java:2627)
i basing code example on this: how use arrayadapter<myclass>.
arraylist<question> list = new arraylist<question>(); for(int i=1; <= 3; i++) { path = getassets().open("quiz"+i+".txt"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } reader reader = new resultsreader(path); reader.read(); string str = ((resultsreader)reader).getinput(); string data[] = str.split("<.>"); question q = new question(); q.question = data[0]; q.answer = integer.parseint(data[1]); q.choice1 = data[2]; q.choice2 = data[3]; q.choice3 = data[4]; list.add(q); }
when
string data[] = str.split("<.>");
it going return array of uncertain length, unless know input correct time going run issues when below:
q.question = data[0]; q.answer = integer.parseint(data[1]); q.choice1 = data[2]; q.choice2 = data[3]; q.choice3 = data[4];
perhaps should think of technique don't rely on calling explicit index's of array.
for example:
if(data.length == 4){ q.question = data[0]; q.answer = integer.parseint(data[1]); q.choice1 = data[2]; q.choice2 = data[3]; q.choice3 = data[4]; } else { log.e("yourapp", "data not split file correctly"); }
or add more debug before use split data array:
for(int i=0; < data.length; i++){ log.d("yourapp", "datareceived:"+data[i]; }
edit
oh yeah issue emulator / phone hasn't got clue what
c:/
is.
Comments
Post a Comment