java.lang.illegalArgumentException 意外显示

java.lang.illegalArgumentException getting displayed Un-expectedly

我试图通过从数据库中获取所有子任务来显示它们的列表视图。我收到非法参数异常。但我很惊讶,因为我正在查询一个不同的 table 并且异常显示其他一些 tables 列不存在。请任何人告诉我发生了什么以及代码中缺少什么:

我的列表查看代码:

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);


            }


        });

    }

我的数据库代码:

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;


    }

我的 ADB 跟踪:

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.access0(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.access0(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)

我的数据库架构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 + "));";

请让我知道缺少什么并帮助我解决这个问题。提前致谢。

您必须检查 SQLiteHelper.UIDCHILD 的字段。如果该字段名为 id,请将其更改为 _id。或者当您查询时:使用 id AS _id 而不是 id。 Listview要求行id命名为_id

要使用需要名为 _id 的列的游标,请编辑您的 table 创建语句并添加名为 _id 的列(或将 UIDCHILD 值更改为“_id”)。

参考documentation