0

I get crash report error for my app. I use imported sqlite database in assets folder. What am I doing wrong? Here's the error from user crash report:

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

And here's my question method from my game class:

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);
                c.moveToFirst();

                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();
            }
     }

I get error on line:

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

Here's my adapter class:

public class TestAdapter 
{
    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public TestAdapter(Context context) 
    {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public TestAdapter createDatabase() throws SQLException 
    {
        try 
        {
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public TestAdapter open() throws SQLException 
    {
        try 
        {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, "open >>"+ mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

    public void close() 
    {
        mDbHelper.close();
    }

     public Cursor getTestData(String whereClause){
             String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";
             return mDb.rawQuery(sql, null);
         }

     public Cursor getTestDataGradovi(long id){
            String sql ="SELECT * FROM tblGradovi WHERE _ID = " + id;
            return  mDb.rawQuery(sql, null);
        }

     public Cursor getTestDataValute(long id){
             String sql ="SELECT * FROM tblValute WHERE _ID = " + id;

             return mDb.rawQuery(sql, null);
         }

}
4

2 回答 2

0

android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0

您的问题是您的c.moveToFirst()方法很可能返回 false (它指示光标是否为空),因此您得到Exception

每当您处理游标时,请始终检查moveToFirst()表示“方便”工具的方法的返回值,如何使用条件检测游标的有效性并将您的“操作”包装到此条件:

List<Answer> labels = new ArrayList<Answer>();
if (c != null) {
   if (c.moveToFirst()) {
      // do your stuff
      ...
      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));
      ...
   }
   else {
     // Cursor is empty
   }
}
else {
   // Cursor is null
}

注意:当您想从 Cursor 中包含的特定列中检索值时,我建议您使用getColumnIndex(<columnName>)方法来避免“错字”和“索引”问题。这是非常好的和有效的做法。

于 2013-05-13T16:39:54.143 回答
0

您正在尝试访问Cursor数据,但它似乎是空的。使用c.moveToFirst()来实际检查光标是否为空 - 它返回一个布尔指示:boolean isFilledWithData = c.moveToFirst()

于 2013-05-13T16:40:30.920 回答