Couldn't read row 0, col -1 from CursorWindow

删除回忆录丶 提交于 2019-12-02 16:53:27

问题


I'm trying to get records in my table. I'm using this code

        if(c!=null){
            if(c.moveToFirst()) {

                String tipId = c.getString(c.getColumnIndex(DAO.TIP_ID)).toString().trim();                 
                System.out.println("tipId: "+ tipId);

            }else{
                // Handle no rows returned
                System.out.println("Handle no rows returned");
            }
        }else{
            System.out.println("debug Cursor , cannot be created");
        }

But it gives me the following exception

Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

and it refers to that line:

String tipId = c.getString(c.getColumnIndex(DAO.TIP_ID)).toString().trim();

hope anyone helps me to fix this. Thanks in advance.


回答1:


In your code

  c.getColumnIndex(DAO.TIP_ID)

is returning -1. This means value of DAO.TIP_ID doesnot match with any column name of table .Check if this value is correct and matches with a column name of the table you are accessing

add following line before the line which causes problem and post the Logcat output here

 Log.v("out-data",DAO.TIP_ID+" "+c.getColumnIndex(DAO.TIP_ID)+" "+c.getColumnName(0));

(add import statement import android.util.Log; in beginning of your code)




回答2:


The query selection does not contain the column DAO.TIP_ID.




回答3:


You should fix this to solve this error message

"Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it."

it works in my code.

  1. Try to position cursor by moveToFirst before reading data from it.

  2. check for null. e,g; if (c != null && c.moveToFirst()) {}

  3. check for count. e,g; (c != null && c.getCount() >0 && c.moveToFirst()){}



来源:https://stackoverflow.com/questions/20530912/couldnt-read-row-0-col-1-from-cursorwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!