无法添加到我的sqlite表(可能不是NULL(代码19))

时间:2013-11-21 01:04:02

标签: android database sqlite

我正在尝试通过添加一些文本来更新我的Sqllite表但是当我点击保存时它不会出现并且会显示在日志中

11-20 18:54:47.196: E/SQLiteDatabase(27886): Error inserting summary=tryig to insert text
11-20 18:54:47.196: E/SQLiteDatabase(27886): android.database.sqlite.SQLiteConstraintException: todo.category may not be NULL (code 19)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at com.oxpheen.notescheduler.contentprovider.MyTodoContentProvider.insert(MyTodoContentProvider.java:101)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.content.ContentProvider$Transport.insert(ContentProvider.java:201)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.content.ContentResolver.insert(ContentResolver.java:866)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at com.oxpheen.noteschedulerfree.SchedulenoteDialog.saveState(SchedulenoteDialog.java:273)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at com.oxpheen.noteschedulerfree.SchedulenoteDialog.onPause(SchedulenoteDialog.java:251)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.Activity.performPause(Activity.java:5206)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3130)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3099)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3077)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.ActivityThread.access$800(ActivityThread.java:153)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1264)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.os.Looper.loop(Looper.java:137)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
11-20 18:54:47.196: E/SQLiteDatabase(27886):    at dalvik.system.NativeStart.main(Native Method)

这是第251行:

   @Override
  protected void onPause() {
    super.onPause();
    saveState(); /////THIS IS IT
  }

这是我的saveState方法(第273行包含一堆////)

  private void saveState() {
    //String category = (String) mCategory.getSelectedItem();
    String summary = mTitleText.getText().toString();
    //String description = mBodyText.getText().toString();


    if (summary.length() == 0) {
      return;
    }

    ContentValues values = new ContentValues();
    //values.put(TodoTable.COLUMN_CATEGORY, category);
    values.put(TodoTable.COLUMN_SUMMARY, summary);
   // values.put(TodoTable.COLUMN_DESCRIPTION, description);

    if (todoUri == null) {
      // New note
      todoUri = getContentResolver().insert(MyTodoContentProvider.CONTENT_URI, values); 
          //This is line 273 above
    } else {
      // Update note
        Toast.makeText(ScheduleTweetDialog.this, "didnt do content uri",
                Toast.LENGTH_SHORT).show();
     getContentResolver().update(todoUri, values, null, null);
    }
  }

如果你可以帮助我,那就太好了。我只是无法弄清楚,已经持续了好几个小时。非常感谢!我会立即发布任何其他需要的东西!

编辑:如果我取消注释,我得到这个不能暂停活动错误。

    11-20 19:13:33.375: E/AndroidRuntime(2717): FATAL EXCEPTION: main
11-20 19:13:33.375: E/AndroidRuntime(2717): java.lang.RuntimeException: Unable to pause activity {com.oxpheen.tweetschedulerfree/com.oxpheen.tweetschedulerfree.ScheduleTweetDialog}: java.lang.NullPointerException
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3099)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3077)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread.access$800(ActivityThread.java:153)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1264)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.os.Looper.loop(Looper.java:137)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread.main(ActivityThread.java:5227)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at java.lang.reflect.Method.invoke(Method.java:511)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at dalvik.system.NativeStart.main(Native Method)
11-20 19:13:33.375: E/AndroidRuntime(2717): Caused by: java.lang.NullPointerException
11-20 19:13:33.375: E/AndroidRuntime(2717):     at com.oxpheen.tweetschedulerfree.ScheduleTweetDialog.saveState(ScheduleTweetDialog.java:266)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at com.oxpheen.tweetschedulerfree.ScheduleTweetDialog.onPause(ScheduleTweetDialog.java:262)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.Activity.performPause(Activity.java:5206)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
11-20 19:13:33.375: E/AndroidRuntime(2717):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3130)
11-20 19:13:33.375: E/AndroidRuntime(2717):     ... 13 more

我的暂停方法

  @Override
  protected void onPause() {
    super.onPause();
    saveState();
  }

Redone savestate方法

 private void saveState() {
    String category = (String) mCategory.getSelectedItem();
    String summary = mTitleText.getText().toString();
    String description = mBodyText.getText().toString();

    // only save if either summary or description
    // is available

    if (description.length() == 0 && summary.length() == 0) {
      return;
    }

    ContentValues values = new ContentValues();
    values.put(TodoTable.COLUMN_CATEGORY, category);
    values.put(TodoTable.COLUMN_SUMMARY, summary);
    values.put(TodoTable.COLUMN_DESCRIPTION, description);

    if (todoUri == null) {
      // New todo
      todoUri = getContentResolver().insert(MyTodoContentProvider.CONTENT_URI, values);
    } else {
      // Update todo
      getContentResolver().update(todoUri, values, null, null);
    }
  }

2 个答案:

答案 0 :(得分:1)

您注释了将值插入CATEGORY:

//values.put(TodoTable.COLUMN_CATEGORY, category);

因此它尝试向此列插入null,显然这不是此列的有效值(请检查表定义以了解原因)。因此,您需要在此列中插入一些值(例如取消注释此行)

答案 1 :(得分:1)

您显然已使用todo.category定义了您的表,而不是null。这意味着您无法在该列中插入空值。

最有可能发生的事情(您可以使用调试器进行验证)是,在saveState()中,category为null。如果在程序的其余部分中这是可以接受的,那么你可以做的是:

values.put (TodoTable.COLUMN_CATEGORY, category == null ? "" : category);

这会在列中放入一个空字符串,而不是尝试放入null。

如果要检索该值,可以执行以下操作:

String category = cursor.getString (col);
if (category.length == 0)
  category = null;

如果您确实需要区分null和空字符串,那么最好将表定义更改为允许此列中的空值。