gpt4 book ai didi

java - android.database.CursorIndexOutOfBoundsException...再一次

转载 作者:行者123 更新时间:2023-11-29 05:44:08 30 4
gpt4 key购买 nike

所以,我的应用程序的用户崩溃报告中有这个错误。这是报告:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard.nextQuestionGrad(Kviz10Hard.)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard.access$1(Kviz10Hard.)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard$2.run(Kviz10Hard.java:68)
at android.os.Handler.handleCallback(Handler.)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.)
at android.app.ActivityThread.main(ActivityThread.)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.)
at dalvik.system.NativeStart.main(Native Method)

这里是提到的代码:

private void nextQuestionGrad() {

flag.setVisibility(View.GONE);
dodatnoPitanje.setVisibility(View.VISIBLE);

TestAdapter mDbHelper = new TestAdapter(this);
DataBaseHelper myDbHelper = new DataBaseHelper(this);

if(!myDbHelper.checkDataBase()){
mDbHelper.createDatabase();
}

try{

mDbHelper.open();

Cursor c = mDbHelper.getTestDataGradovi(mCurrentID);

List<Answer> labels = new ArrayList<Answer>();

labels.add(new Answer(c.getString(2), true));
labels.add(new Answer(c.getString(3), false));
labels.add(new Answer(c.getString(4), false));
labels.add(new Answer(c.getString(5), false));

tacanOdg = c.getString(2);

Collections.shuffle(labels);

dodatnoPitanje.setText(c.getString(1));

bOdgovor1.setText(labels.get(0).option);
bOdgovor1.setTag(labels.get(0));
bOdgovor1.setOnClickListener(clickListenerGrad);

bOdgovor2.setText(labels.get(1).option);
bOdgovor2.setTag(labels.get(1));
bOdgovor2.setOnClickListener(clickListenerGrad);

bOdgovor3.setText(labels.get(2).option);
bOdgovor3.setTag(labels.get(2));
bOdgovor3.setOnClickListener(clickListenerGrad);

bOdgovor4.setText(labels.get(3).option);
bOdgovor4.setTag(labels.get(3));
bOdgovor4.setOnClickListener(clickListenerGrad);

score.setText("Score: " + brojacTacnihOdgovora);

}finally{
mDbHelper.close();
}
}

还有我正在用于此类的适配器类的一部分:

public Cursor getTestDataGradovi(long id){
try{
String sql ="SELECT * FROM tblGradovi WHERE _ID = " + id;

Cursor c = mDb.rawQuery(sql, null);
if (c!=null){
c.moveToNext();
}
return c;
}catch (SQLException mSQLException){
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}

谁能告诉我问题出在哪里?错误行 (Kviz10Hard.) 在这里:

labels.add(new Answer(c.getString(2), true));

最佳答案

在尝试从 Cursor 对象获取任何数据之前,您需要迭代结果,或者如果游标仅包含一个值,则获取第一个结果。例如,要在所有结果之间进行迭代,您应该这样做:

String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
if(cursor.getCount() > 0){
for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
// get your data here
}
}
cursor.close(); // close your cursor when you don't need it anymore
}

或者另一种方法:

String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
if(cursor.getCount() > 0){
do {
// get value
} while(cursor.moveToNext());
}
cursor.close(); // close your cursor when you don't need it anymore
}

或者如果您非常确定结果应该只是一个对象,具体取决于您的 sql 语句,您应该调用 cursor.moveToFirst(); 在从您的 Cursor 访问任何内容之前> 像这样:

String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
cursor.moveToFirst();
String myString = cursor.getString(cursor.getColumnIndex("title"));
cursor.close(); // close your cursor when you don't need it anymore
}

编辑:

如果你真的想在你的数据库类中创建这样的函数,我会这样做:

public static Cursor getUserTitles(int userId){
String sql = "SELECT * FROM myTable WHERE userId=?";
return mDbHelper.rawQuery(sql, new String[]{Integer.toString(userId)});
}

第二次编辑:

将您的 getTestDataGradovi 更改为:

public Cursor getTestDataGradovi(long id){
String sql ="SELECT * FROM tblGradovi WHERE _ID=?";
return mDb.rawQuery(sql, new String[]{Integer.toString(id)});
}

并在 Cursor c = mDbHelper.getTestDataGradovi(mCurrentID); 之后添加 c.moveToFirst();;

关于java - android.database.CursorIndexOutOfBoundsException...再一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417055/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com