我在将 ContentValues
输入到我的程序中的数据库时遇到了一些问题,事实证明这很难理解。我有一个 ContentValues
对象,我向其中添加了大约 50 个条目并且一切正常,除了一个存储为“null”的条目。根据 LogCat 输出,我知道进入 ContentValues
的数据不为空。
相关代码:
if (database.isOpen() && !database.isReadOnly()) { //Check that the database is open and not read only
Log.d(TAG, "Entry string: " + data.getEntryString());
Log.d(TAG, "Key: " + Helper.COLUMN_ENTRY_STRING);
ContentValues values = new ContentValues(); //Create a new ContentValues object to store key value pairs for insertion
//Various values.put() calls...
values.put(Helper.COLUMN_ENTRY_STRING, data.getEntryString()); //Should put the String "Something"
Log.d(TAG, "Entry string from values: " + values.getAsString(Helper.COLUMN_ENTRY_STRING));
//Proceed with database insert here
}
LogCat 响应:
07-27 08:58:47.909: D/Handler(18020): Entry string: Something
07-27 08:58:47.909: D/Handler(18020): Key: Entry_String
07-27 08:58:47.909: D/Handler(18020): Entry string from values: null
我已经多次检查代码,但找不到任何解释。任何帮助将不胜感激。
最佳答案
我也遇到过这个问题。
我的情况是因为我在单元测试中使用了 ContentValue
。 ContentValue
是一个android组件,应该在android环境下使用,但是我在没有android实现的单元测试中使用了它。因此,当我调用 contentValue.put(key, value)
和 contentValue.get(key)
时,由于实现为空,所以什么也没有发生。
我不确定您是否在单元测试或其他没有 android 实现的情况下运行您的代码。也许我的解决方案可以给你一些想法。
关于android - ContentValues 条目正在转换为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11691642/