【问题标题】:Cursor Index out o fBounds Exception :Index 11 requested, with a size of 11光标索引超出范围异常:请求索引 11,大小为 11
【发布时间】:2018-10-05 11:41:41
【问题描述】:

每当尝试获取 getSelectedCustomerPhone() 时,我都会随机遇到一个问题,然后会出现随机光标索引超出绑定异常。 这段代码有什么问题吗?我找不到错误。

private String getSelectedCustomerPhone() {

        myCursor.moveToPosition(selectedCustPosition);

        String phone = 
               myCursor.getString(myCursor.getColumnIndex("cust_phone"));
        if (phone != null) return phone;
        return "";
    }

【问题讨论】:

标签: android sqlite cursor


【解决方案1】:

selectedCustPosition 似乎超出了光标范围 [0, Cursor.getCount() - 1]。试着理解它为什么会发生。 作为防止崩溃的解决方法,您可以添加检查

if (0 <= selectedCustPosition && selectedCustPosition < myCursor.getCount()) {
   myCursor.moveToPosition(selectedCustPosition);
   // ...
}

但这只是一种解决方法,它更有可能返回错误的电话号码。更好地理解真正的问题:为什么selectedCustPosition 不正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多