我有一個錯誤,我希望你能幫我解決。我回來一個字符串,由':'從getdata();方法分開。 一切工作正常在模擬器上,但是當我的設備上運行的應用程序,該方法返回一個空字符串,我得到一個錯誤:IndexOutOfBoundsException:length = 1;指數= 2.`IndexOutOfBoundsException:length = 1; index = 2
代碼:
Databaseengine info = new Databaseengine(this);
info.open();
String data = info.getdata();
info.close();
String[] values = null;
values = data.split(":");
tvans1.setText(values[4]);
tvans2.setText(values[5]);
tvans3.setText(values[6]);
tvans4.setText(values[7]);
的方法是這樣的:
public String getdata() {
// TODO Auto-generated method stub
String[] columns = new String [] { KEY_ROWID, KEY_CATEGORY, KEY_QUESTION, KEY_ANSWER, KEY_ALTONE, KEY_ALTTWO, KEY_ALTTHREE, KEY_ALTFOUR };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null , null , null, "RANDOM() LIMIT 1");
String result = "";
//The cursor will be looking for these columns:
int iRow = c.getColumnIndex(KEY_ROWID);
int iCat = c.getColumnIndex(KEY_CATEGORY);
int iQuiz = c.getColumnIndex(KEY_QUESTION);
int iAns = c.getColumnIndex(KEY_ANSWER);
int iAlt01 = c.getColumnIndex(KEY_ALTONE);
int iAlt02 = c.getColumnIndex(KEY_ALTTWO);
int iAlt03 = c.getColumnIndex(KEY_ALTTHREE);
int iAlt04 = c.getColumnIndex(KEY_ALTFOUR);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + ":"
+ c.getString(iCat) + ":" + c.getString(iQuiz) + ":"
+ c.getString(iAns) + ":" + c.getString(iAlt01) + ":"
+ c.getString(iAlt02) + ":" + c.getString(iAlt03) + ":"
+ c.getString(iAlt04) + "\n";
}
c.close();
return result;
}
請幫助我,我去之前精神在這裏:-)
至少後的字符串數據的內容一試 – njzk2
添加data'的'打印輸出,看看很多':'它包含 – Andremoniy
你的'info.getDate()'返回什麼?打印並看看。 –