我正在嘗試從 SQLite 數據庫獲取 checkTextView 狀態,但它導致應用程序崩潰。下面是實現的代碼: Cursor c = db.rawQuery("SELECT * FROM list", null); int foundIndex = c.getColumnIndex("found"); while (c != null) { c.getString(foundIndex); c.moveToNext(); } String[] foundList = new String[foundIndex]; arrayAdapter.notifyDataSetChanged(); for (String found : levelOneListList){ if (levelOneListList == foundList){ levelOneListView.setItemChecked(found.indexOf(foundIndex), true); } }}它給出了這個錯誤:java.lang.RuntimeException:無法啟動活動 ComponentInfo{com.example.woodlandwanderer/com.example.woodlandwanderer.levelOneActivity}:android.database.CursorIndexOutOfBoundsException:請求索引 -1,大小為 0
2 回答

九州編程
TA貢獻1785條經驗 獲得超4個贊
正如錯誤所示,您的光標大小為 0,并且您正在嘗試首先訪問。
在循環之前添加以下檢查。
if (c.moveToNext() && c.getCount()>0) {

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
像這樣讀取光標數據??雌饋砟愕墓鈽舜笮∈?0。
if (c != null && c.moveToFirst()){
do {
int foundIndex = c.getColumnIndex("found");
c.getString(foundIndex);
} while (c.moveToNext());
c.close(); // close your db cursor
}
// list update and set checkbox value here
添加回答
舉報
0/150
提交
取消