java.lang.numberFormatException:无效的int“_id”

时间:2015-03-10 06:29:46

标签: android android-sqlite

我正在尝试将数据添加到名为User Table的表中。它有两列Task和SubTask。任务应该保存任务表的_id值,SubTask应该保存SubTask表的_id值。因此,我已将这两列作为此表的外键引用。现在我无法在此表中插入值。我得到了上述异常。请任何人都可以告诉我必须采取哪些措施来克服这个问题。欢迎所有建议。提前谢谢。

UserTable的架构:

public static final String CREATE_TABLE_USER = " CREATE TABLE " + TABLE_USER + " ( " + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            TASK + " INTEGER, " + SUB_TASK + " INTEGER, " + " FOREIGN KEY (" + TASK + ") REFERENCES " + TABLE_NAME + " (" + UID + " )" +
            " FOREIGN KEY (" + SUB_TASK + ") REFERENCES " + TABLE_NAME_CHILD + " (" + UIDCHILD + ") );";

为插入操作编写的数据库代码:

public long addTaskPosition( int task_number, int sub_task_number) {

        db = helper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(SQLiteHelper.TASK, task_number);
        contentValues.put(SQLiteHelper.SUB_TASK, sub_task_number);
        id2 = db.insert(SQLiteHelper.TABLE_USER, null, contentValues);


        Log.d("PaNa", "The value of Id is " + id);

        db.close();
        return id2;


    }

在活动中编写的代码,用于设置db的值:

 int taskNumber = Integer.parseInt(String.valueOf(SQLiteHelper.UID));
        Log.d("Pana" ,  "The value of taskNumber is " +taskNumber);
        int subTaskNumber = Integer.parseInt(String.valueOf(SQLiteHelper.UIDCHILD.toString()));


        long id2 = sqliteHelper.addTaskPosition(taskNumber, subTaskNumber);

        if (id2 < 0) {

            Toast.makeText(getApplicationContext(), "Insert Unsuccessful", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Insert Successful", Toast.LENGTH_LONG).show();
        }

Adb Trace:

 Process: com.ms.t.tms, PID: 1835
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            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.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            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.NumberFormatException: Invalid int: "_id"
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parse(Integer.java:410)
            at java.lang.Integer.parseInt(Integer.java:367)
            at java.lang.Integer.parseInt(Integer.java:334)
            at com.ms.t.tms.InsertSubTaskActivity.addUser(InsertSubTaskActivity.java:496)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            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)

请让我知道必须采取哪些措施才能解决这个问题。我是android的新手。请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:1)

如果你想要SQLiteHelper.UIDCHILD和SQLiteHelper.UID的值,你可以在相应的表上执行重新查询,并使用以下方法迭代它:

   ArrayList uid=new ArrayList<Integer>();
   Cursor cur=db.rawQuery("SELECT _id FROM yourtablename",null);
   if (cur.getCount() > 0) 
      {
      cur.moveToFirst();
       do 
        {
        uid.add(cur.getInt(0));
        }
      while (cur.moveToNext());
     cur.close();
      }

和另一个相同。你可以通过这样的意图将这个arraylist传递给一个活动

Intent intent=new Intent(Activity_context,youractivity_class);
intent.putStringArrayListExtra("uid",uid);
Activity_context.startActivity(intent);