-4
     E/AndroidRuntime: FATAL EXCEPTION: main

     Process: com.example.bestbuy.mytry, PID: 32644
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bestbuy.mytry/com.example.bestbuy.mytry.DisplayActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
                                                                        Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
                                                                           at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
                                                                           at android.widget.CursorAdapter.init(CursorAdapter.java:172)
                                                                           at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
                                                                           at com.example.bestbuy.mytry.CustomAdapter.<init>(CustomAdapter.java:19)
                                                                           at com.example.bestbuy.mytry.DisplayActivity.onCreate(DisplayActivity.java:18)
                                                                           at android.app.Activity.performCreate(Activity.java:6033)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:135) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:372) 

This exception is arising when i retrieve data from sqlitedatabase and displaying it in ListView. I am not able to interpret this. please help in understanding it and what this actually mean in context with my application. If you want some more data let me know. thank you

This is my sqlitedatabase code-

    public class DatabaseHelper extends SQLiteOpenHelper {
     public static final String DATABASE_NAME="AppDB";
     public static final int DATABASE_VERSION=1;
     public static final String TABLE_NAME="Users";
     public static final String Column1="id";
     public static final String Column2="task";
     public static final String Column3="ddate";
     public static final String Query="CREATE TABLE "+TABLE_NAME+"("+Column1+" INTEGER PRIMARY KEY AUTOINCREMENT,"+Column2+" TEXT NOT NULL,"+Column3+" TEXT NOT NULL)";
public DatabaseHelper(Context context) {

    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public boolean insertValues(String task,String datee) {
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(Column2,task);
    values.put(Column3,datee);
    long rows=db.insertOrThrow(TABLE_NAME,null,values);
    if(rows>0){
        return true;
    }
    else
        return false;
}
 public Cursor retrieveValues() {
    SQLiteDatabase db=this.getWritableDatabase();
    Cursor cursor=db.rawQuery("SELECT * FROM Users",null);
    return cursor;
}

This is my customAdapter code:

     public class CustomAdapter extends CursorAdapter {
     TextView task,daate;
     public CustomAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
     }

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.adapter, parent, false);
}

// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
    // Find fields to populate in inflated template
    task=(TextView)view.findViewById(R.id.dynamicTask);
    daate=(TextView)view.findViewById(R.id.dynamicDate);
    String Task=cursor.getString(cursor.getColumnIndex("task"));
    String Daate=cursor.getString(cursor.getColumnIndex("ddate"));
    task.setText(Task);
    daate.setText(Daate);
}

}

And this is my DisplayActivity code:

 public class DisplayActivity extends AppCompatActivity {
  ListView tasklist;
  DatabaseHelper db;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show);
    tasklist=(ListView)findViewById(R.id.taskList);
    db=new DatabaseHelper(this);
    Cursor cursor=db.retrieveValues();
    CustomAdapter todoadapter=new CustomAdapter(this,cursor);
    tasklist.setAdapter(todoadapter);
}

}

Suraj
  • 41
  • 7
  • isn't `column '_id' does not exist` enough ? – Blackbelt Oct 17 '17 at 07:36
  • i dont have such column _id in my database. thats why – Suraj Oct 17 '17 at 07:37
  • Then add one. It is required. There similar answers to this, search it before you open a question. – Gokhan Arik Oct 17 '17 at 07:39
  • i know! i have three columns in my database named "id", "task", "date" and i also put id column as an autoincrement and primary key, but its showing this exception after having these things also. – Suraj Oct 17 '17 at 07:43
  • Ok post also your code where you configure the sqlite database, your `CustomAdapter` and `DisplayActivity`. You have to provide as much info as you can in order to get an answer. – pleft Oct 17 '17 at 07:44
  • and `CursorAdapter` code please – pleft Oct 17 '17 at 07:50
  • I think CursorAdapter is an inbuilt class. How to show that? I just extended it. – Suraj Oct 17 '17 at 07:55

1 Answers1

0

To use the CursorAdapter class your database must have _id column as official documentation states:

Adapter that exposes data from a Cursor to a ListView widget.

The Cursor must include a column named "_id" or this class will not work. Additionally, using MergeCursor with this class will not work if the merged Cursors have overlapping values in their "_id" columns.

So replace you id column with _id in your table creation script to comply with the requirement for using the CursorAdapter class.

pleft
  • 7,567
  • 2
  • 21
  • 45