java.lang.illegalArgumentException获取显示意外

时间:2015-03-12 13:36:04

标签: android listview simplecursoradapter

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

我的列表查看代码:

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

该表的数据库架构:

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

请告诉我遗漏的内容并帮助我克服这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

参考documentation