我遇到了一个错误,希望你能帮我解决。
我从方法 getdata() 返回一个字符串,由 ':' 分隔;在模拟器上一切正常,但是在我的设备上运行应用程序时,该方法返回一个空字符串并且我收到一个错误:IndexOutOfBoundsException: length=1; 索引=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;
}
请帮助我,在我在这里发疯之前:-)