异常:CursorIndexOutOfBoundsException:请求索引0,大小为0

时间:2018-07-15 11:21:00

标签: android sqlite

所以昨天我在卸载并重新安装我的应用程序以检查是否正确请求了权限时才遇到此问题。

在该应用正常运行之前,但现在我无法从数据库中获取任何信息。尝试从游标获取数据时,总是会出现此错误。

Process: com.example.asusx556u.myapplication, PID: 1720
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asusx556u.myapplication/com.example.asusx556u.myapplication.RingtonesPlayLists}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.asusx556u.myapplication.RingtonesPlayLists.checkRingtones(RingtonesPlayLists.java:54)
at com.example.asusx556u.myapplication.RingtonesPlayLists.onCreate(RingtonesPlayLists.java:34)

checkRingtones:

public void checkRingtones() {
        if(ringtonesCursor == null) {
            return;
        }

        boolean bModified = false;
        DatabaseHelper ringtonesDB = new DatabaseHelper(this);

        if(ringtonesCursor.moveToFirst())
            Log.v("SL", "Moved successfully to first row.");
        do {
            File ringtoneFile = new File(ringtonesCursor.getString(2));
            if(!ringtoneFile.exists()) {
                bModified = true;
                ringtonesDB.delete(ringtonesCursor.getInt(0));
                Log.e("SL", "Ringtone \"" + ringtonesCursor.getString(2) +
                "\" cannot be found and therefore will be deleted!");
            }
        } while (ringtonesCursor.moveToNext());

        if(bModified)
            getRingtones();
    }

getRingtones:

public void getRingtones() {
        DatabaseHelper ringtonesDB = new DatabaseHelper(this);
        ringtonesCursor = ringtonesDB.getAll();
    }

database.getAll:

public Cursor getAll() {
        SQLiteDatabase db = _openHelper.getReadableDatabase();
        if (db == null) {
            return null;
        }
        return db.rawQuery("select * from RINGTONES_LITE order by _id", null);
    }

database.onCreate:

        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table RINGTONES_LITE (_id integer primary key autoincrement, NAME text, URI text unique)");
        }

1 个答案:

答案 0 :(得分:1)

您的问题可能是由于没有检查空的游标,所以游标有0行,因此没有从中获取任何数据的列(即offset 0)。

这是因为检查Cursor为null毫无用处。如果Cursor没有行,则Cursor将为空而不是null(Cursor data方法将返回0)。

可以检测到此情况的检查this.state(如果Cursor没有行,将返回false)。仅限于Log语句。仍然输入do子句,因此结果是没有偏移量0,因为没有行。

一种修复方法是使用:-

getCount()

或者::

moveToFirst