java.lang.IllegalArgumentException: column '_id' does not exist

前端 未结 5 1494
暖寄归人
暖寄归人 2020-12-15 16:37

I\'m trying to debug my application on a real device but I get this error:

ERROR/AndroidRuntime(981): Caused by: java.lang.IllegalArgumentException:

相关标签:
5条回答
  • 2020-12-15 16:52

    I have had similar problem because I was not adding the _id column to the projection argument, so adding _id to the projections argument of the query was the solution. (commented by @nobugs)


    Example:

     String[] projections = {"_id", "name", "age"};
    
     Cursor cursor = db.query(domainClass.getSimpleName(), projections, 
         null, null, null, null, null);
    
    0 讨论(0)
  • 2020-12-15 17:06

    You are trying to use a cursor that REQUIRES a column called _id. Its as simple as editing your table creation statement and adding a column called _id.

    Its declartion looks something like this:

    _id INTEGER PRIMARY KEY AUTOINCREMENT
    

    Add this and you will then be able to use it. I believe this is a requirement that is required in order to use a SimpleCursorAdapter.

    UPDATE

    "CREATE TABLE IF NOT EXISTS contact_data( _id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, duration_sum TIME, date DATE, current_time TIME, cont INTEGER, type VARCHAR, month VARCHAR(50), day VARCHAR(50), year VARCHAR(50));"
    

    Solution: add a space between the left parenthesis '(' and _id

    0 讨论(0)
  • 2020-12-15 17:12

    I did this and solved my problem.

    Before

    SELECT id

    After

    SELECT id as _id

    0 讨论(0)
  • 2020-12-15 17:14

    First of all uninstall the app, and then do following steps:

    1. Clean the project
    2. rebuild he project
    3. debug the app(Shift+F9)
    0 讨论(0)
  • 2020-12-15 17:15

    CursorAdapter always requires a _id column to be present. So, in the list of projections used for creating Cursor, you need to add _id column.

    0 讨论(0)
提交回复
热议问题