Search code examples
androidlistviewsimplecursoradapter

java.lang.illegalArgumentException getting displayed Un-expectedly


I am trying to display list view of all my sub tasks by fetching them from database. I am getting an illegal argument exception. But I am surprised because I am querying a different table and the exception displays some other tables column does not exist. Can any one of please let me know what is happening and what is missing in the code:

My List View code:

public void populateListView() {

        SQLiteDataBaseAdapter adapter = new SQLiteDataBaseAdapter(this);

        Cursor cursor = adapter.getAllSubTaskData();

        // Log.d("Pana", "The value of cursor is " +Integer.parseInt(String.valueOf(cursor.toString())));

        String[] fromFieldNames = new String[]{SQLiteHelper.UIDCHILD, SQLiteHelper.SUB_TASK_NAME};
        int[] toViewIds = new int[]{R.id.textViewNumber, R.id.textViewName};

        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(this, R.layout.custom_list_row, cursor, fromFieldNames, toViewIds, 0);
        final ListView listView = (ListView) findViewById(R.id.listViewSubTask);
        listView.setAdapter(myCursorAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Toast.makeText(getApplicationContext(), "The position of the item clicked is " + position, Toast.LENGTH_LONG).show();





                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), SubTaskDetail.class);
                intent.putExtra("position", Integer.toString(position + 1));  //position starts from 0, but in db row starts from 1
                startActivity(intent);


            }


        });

    }

My DB code:

public Cursor getAllSubTaskData() {
        db = helper.getWritableDatabase();
        String[] columns = {SQLiteHelper.UIDCHILD,
                SQLiteHelper.SUB_TASK_NAME,
                SQLiteHelper.CONTACT_NAME,
                SQLiteHelper.CONTACT_NUMBER,
                //SQLiteHelper.CONTACT_EMAIL,
                SQLiteHelper.DESCRIPTION,
                SQLiteHelper.REMARKS,
                SQLiteHelper.DATE,
                SQLiteHelper.TIME,
                SQLiteHelper.ESTIMATED_COMPLETION_DATE,
                SQLiteHelper.ESTIMATED_COMPLETION_TIME,
                SQLiteHelper.ACTUAL_COMPLETION_DATE,
                SQLiteHelper.ACTUAL_COMPLETION_TIME,
                SQLiteHelper.NOTIFY_DATE,
                SQLiteHelper.NOTIFY_TIME};
        Cursor cursor = db.query(SQLiteHelper.TABLE_NAME_CHILD, columns, null, null, null, null, null);

        }

        if (cursor != null) {
            cursor.moveToFirst();
        }

        return cursor;


    }

My ADB trace:

03-12 18:57:39.189    1502-1502/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ms.t.tms, PID: 1502
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ms.t.tms/com.ms.t.tms.SubTaskList}: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     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 android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at com.ms.t.tms.SubTaskList.populateListView(SubTaskList.java:57)
            at com.ms.t.tms.SubTaskList.onCreate(SubTaskList.java:33)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

My DB schema for that table:

 public static final String CREATE_TABLE_CHILD = " CREATE TABLE " + TABLE_NAME_CHILD +
            "(" + UIDCHILD + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SUB_TASK_NAME + " VARCHAR(250)," + CONTACT_NAME + " VARCHAR(250)," + CONTACT_NUMBER + " VARCHAR(250),"
            + CONTACT_EMAIL + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250),"
            + DATE + " VARCHAR(250)," + TIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_DATE + " VARCHAR(250), " + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), "
            + ACTUAL_COMPLETION_DATE + " VARCHAR(250), " + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_DATE + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250), " + SUB_TASK_NUMBER + " VARCHAR(250), "
            + " FOREIGN KEY (" + UIDCHILD + ") REFERENCES " + TABLE_NAME + " (" + TASK_NAME + "));";

Please let me know what is missing and help me to overcome this issue. Thanks in advance.


Solution

  • You have to check the field of SQLiteHelper.UIDCHILD. If that field is named as id change it to _id. Or when you are querying: instead of id use id AS _id. Listview requires row id to be named as_id