【问题标题】:How to fix IndexOutOfBoundsException : invalid index 0 size is 0如何修复 IndexOutOfBoundsException:无效索引 0 大小为 0
【发布时间】:2015-10-30 05:35:38
【问题描述】:

我将yes|no|maybe 的响应存储到userrelation 表中。
为此,我在DBCONTRACT 中创建了一个表,并在db helper 类中获取值。

当我获取值并存储到另一个变量中时,它会抛出此错误

这是userRelation 表的 SQL 查询

 public static abstract class RingeeUserRelationTable implements BaseColumns {

        public static final String TABLE_NAME = "user_relation";
        public static final String COL1_EVENT_USER_ID = "EVENT_USER_ID";
        public static final String COL2_EVENT_ID = "EVENT_ID";
        public static final String COL3_RINGEE_USER_ID = "RINGEE_USER_ID";
        public static final String COL4_IS_ATTENDING = "IS_ATTENDING";
        public static final String COL5_IS_DELETE = "IS_DELETE";

        public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY," + COL1_EVENT_USER_ID + INTEGER_TYPE + COMMA_SEP + COL2_EVENT_ID + INTEGER_TYPE
                + COMMA_SEP + COL3_RINGEE_USER_ID + INTEGER_TYPE + COMMA_SEP + COL4_IS_ATTENDING + INTEGER_TYPE + COMMA_SEP + COL5_IS_DELETE + INTEGER_TYPE + ")";

        public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

        public static final String RETRIVE_ALL_USER_DATA = "SELECT " + COL1_EVENT_USER_ID + COMMA_SEP + COL2_EVENT_ID + COMMA_SEP + COL3_RINGEE_USER_ID + COMMA_SEP + COL4_IS_ATTENDING + COMMA_SEP
                + COL5_IS_DELETE + " FROM " + TABLE_NAME;
    }

这是获取值并设置为userMOS列表的代码

public ArrayList<UserMO> getAllUserRelation() {
        ArrayList<UserMO> userMOs = new ArrayList<UserMO>();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(DatabaseContract.RingeeUserRelationTable.RETRIVE_ALL_USER_DATA, null );
        if (cursor.moveToFirst()) {
            do {
                UserMO userMO = new UserMO();
                userMO.setEventUserId(cursor.getLong(1));
                userMO.setEventId(cursor.getLong(2));
                userMO.setRingeeUserId(cursor.getLong(3));
                userMO.setIsAttending(cursor.getInt(4));
                userMO.setIsDelete(cursor.getInt(5));
                userMOs.add(userMO);
            } while (cursor.moveToNext());

            cursor.close();
        }
        return userMOs;
    }

这是在 Fragment 中获取 isattending 值的代码

    context = getActivity().getApplicationContext();
            dbHelper = new DatabaseHelper(context);
            userMOs = dbHelper.getAllUserRelation();
// I got the error here there is no value in a table but in back end the values are stored 
    int  isAttending = userMOs.get(position).getIsAttending(); 

我正在使用这个isattending 来设置yes|no|maybe 按钮的颜色

  switch(isAttending)
            {
                case 1:
                    yesBtn.setBackgroundColor(Color.YELLOW);
                    noBtn.setBackgroundColor(Color.BLUE);
                    maybeBtn.setBackgroundColor(Color.BLUE);
                    break;
                case 2:
                    yesBtn.setBackgroundColor(Color.BLUE);
                    noBtn.setBackgroundColor(Color.BLUE);
                    maybeBtn.setBackgroundColor(Color.YELLOW);
                    break;
                case 0:
                    yesBtn.setBackgroundColor(Color.BLUE);
                    noBtn.setBackgroundColor(Color.YELLOW);
                    maybeBtn.setBackgroundColor(Color.BLUE);
                    break;
            }

运行此项目时,出现错误:IndexOutOfBoundsException。
请告诉我错误的原因以及如何解决此问题

【问题讨论】:

  • 您至少需要缩小范围。错误发生在哪里?否则,简单而明显的答案是“不要越界!”
  • 请勿发布无关代码。只需发布您认为可能不是问题的代码即可。

标签: java android sqlite


【解决方案1】:

索引基于0
因此,您需要像这样重写代码:

public ArrayList<UserMO> getAllUserRelation() {
    ArrayList<UserMO> userMOs = new ArrayList<UserMO>();
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(DatabaseContract.RingeeUserRelationTable.RETRIVE_ALL_USER_DATA, null );
    if (cursor.moveToFirst()) {
        do {
            UserMO userMO = new UserMO();
            userMO.setEventUserId(cursor.getLong(0));
            userMO.setEventId(cursor.getLong(1));
            userMO.setRingeeUserId(cursor.getLong(2));
            userMO.setIsAttending(cursor.getInt(3));
            userMO.setIsDelete(cursor.getInt(4));
            userMOs.add(userMO);
        } while (cursor.moveToNext());

        cursor.close();
    }
    return userMOs;
}

【讨论】:

    【解决方案2】:

    编写如下代码,如下图:-

    public ArrayList<UserMO> getAllUserRelation() {
            ArrayList<UserMO> userMOs = new ArrayList<UserMO>();
            SQLiteDatabase db = this.getReadableDatabase();
    
            Cursor cursor = db.rawQuery(DatabaseContract.RingeeUserRelationTable.RETRIVE_ALL_USER_DATA, null );
            if (cursor.moveToFirst()) {
                do {
                    UserMO userMO = new UserMO();
                    userMO.setEventUserId(cursor.getLong(1));
                    userMO.setEventId(cursor.getLong(2));
                    userMO.setRingeeUserId(cursor.getLong(3));
                    userMO.setIsAttending(cursor.getInt(4));
                    userMO.setIsDelete(cursor.getInt(5));
                    userMos.add(userMO);
                } while (cursor.moveToNext());
    
                cursor.close();
            }
            return userMOs;
        }
    

    它给你一个数组索引超出范围的异常,因为你没有为你的数组列表分配任何东西。我通过在上面的代码中添加这一行来做到这一点。

    userMos.add(userMo);
    

    【讨论】:

    • 我不认为你的解决方案有用。
    • 等到 OP 提供一些有用的问题信息或告诉 OP 发布有用的信息,然后根据该信息提供解决方案。您当前的解决方案就像Playing the chess in the dark
    • @MD 不,我不是,让 OP 来决定。如果你认为我在黑暗中下国际象棋,那么我认为我只是用我的棋子杀死了你的皇后,把你的国王保存下来作为检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2019-07-19
    • 2016-06-09
    • 1970-01-01
    相关资源
    最近更新 更多