1

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

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));
4

2 回答 2

2

在尝试从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)});
}

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

于 2013-05-07T10:57:23.277 回答
1

您没有检查光标是否包含任何结果。您需要检查c.moveToNext(), 或 checkc.getCount()或类似的返回值。

于 2013-05-07T10:47:33.793 回答