java.lang.numberFormatException: 无效的整数“_id”

java.lang.numberFormatException: Invalid int "_id"

我正在尝试将数据添加到名为用户 Table 的 table。它有两列任务和子任务。 Task 应该持有 Task Table 的 _id 值,而 SubTask 应该持有 SubTask table 的 _id 值。因此,我将这两列作为此 table 的外键引用。现在我无法向此 table 插入值。我可能会遇到上述异常。请任何人让我知道必须做什么才能克服这个问题。欢迎所有建议。提前致谢。

用户架构Table:

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;


    }

在 activity 中编写的代码,用于设置 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();
        }

亚行跟踪:

 Process: com.ms.t.tms, PID: 1835
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View.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.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.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 的新手。请帮帮我。谢谢。

如果您想要 SQLiteHelper.UIDCHILD 和 SQLiteHelper.UID 的值,您可以在各自的 table 上执行 rewquery,并使用以下方法对其进行迭代:

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

与 other.You 相同,可以通过这样的意图将此数组列表传递给活动

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