在我们的项目中,我们使用数据模型来存储从服务接收的数据。所以在特定情况下,当我们将数据插入数据库时,我们使用 objModelClass.getClass().getDeclaredFields() 方法来获取所有字段名称,并且它正确返回类的所有字段名称但是还返回一个名为“$change”的额外字段,该字段在类中不存在。
奇怪的是,在android studio中没有这个问题,但是当我们升级到android studio 2.0时出现了这个问题。
虽然我已经应用了快速修复,但我需要正确修复它。 我想知道为什么会这样?
这个函数使用这个方法
public void insertValuesIntoTable(final String strTableName, ArrayList<Object> alObjClasses,
String strPrimaryKey, String strCompositeKey) {
try {
SQLiteDatabase db_w = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//Iterate through every model class Object in the ArrayList and transfer the contents to the DB
if (alObjClasses != null)
for (Object objModelClass : alObjClasses) {
for (Field field : objModelClass.getClass().getDeclaredFields()) {
//Encrypt value if encryption is enabled & the column is to be encrypted
try {
field.setAccessible(true);
//Test if the table received is Ignore contacts. Minor Hack inserted
//So that the same contact data model can be re used. It checks if the
//Table is of ignored constant or not then checks if the column exists
//in the table or not as that of the field name. Doing this saves
//from receiving a SQLConstraint exception stating, "Column not found"
if(field.getName().equals("$change"))
continue;
if(strTableName.equalsIgnoreCase(ContactConstants.TABLE_IGNORED_CONTACTS))
if(!field.getName().equalsIgnoreCase(ContactConstants.TABLE_IGNORED_CONTACTS_NAMES)
&& !field.getName().equalsIgnoreCase(ContactConstants.TABLE_IGNORED_CONTACTS_NUMBERS)
&& !field.getName().equalsIgnoreCase(ContactConstants.TABLE_IGNORED_CONTACTS_EMAIL)
&& !field.getName().equalsIgnoreCase(ContactConstants.TABLE_IGNORED_CONTACTS_CITY)
&& !field.getName().equalsIgnoreCase(ContactConstants.TABLE_IGNORED_CONTACTS_ID))
continue;
contentValues.put(field.getName(),
(Constants.ENCRYPTION_PREFERENCE && HelperFunctions.isColumnEncrypted(field.getName()))
? HelperFunctions.encryptString((String) field.get(objModelClass))
: (String) field.get(objModelClass));
} catch (IllegalAccessException e) {
// e.printStackTrace();
//Never thrown since field.setAccessible(true); is called before accessing data
}
catch ( ClassCastException e) {
// e.printStackTrace();
//Never thrown since field.setAccessible(true); is called before accessing data
}
}
try {
if (db_w.insert(strTableName, null, contentValues) == -1)
throw new SQLiteConstraintException();
} catch (SQLiteConstraintException e) {
// e.printStackTrace();
Log.i("Error - DB", "Error occurred while trying to add data to "
+ strTableName + " Table, updating data instead.");
//Since the entry exists in the DB, it will be updated instead
updateEntryInDatabase(db_w, strTableName, strPrimaryKey, strCompositeKey, contentValues);
}
}
db_w.close();
} catch (NullPointerException e) {
// e.printStackTrace();
//Is thrown sometimes when the context of the activity which called it is destroyed mid execution
}
catch (SQLiteException e) {
// e.printStackTrace();
//Is thrown sometimes when the context of the activity which called it is destroyed mid execution
}
}
最佳答案
从今天早上开始我就在为这个问题苦苦挣扎。这是由于 Android Studio 2 的新功能 Instant run 将此字段添加到某处,正如调试时可能看到的那样:它实现了接口(interface) com.android.tools.fd.runtime.IncrementalChange .
这个问题可以通过使用 isSynthetic() 方法检查字段来解决:它将为运行时生成的字段返回 true,例如 $change,否则为 false。
关于java - Android Java objModelClass.getClass().getDeclaredFields() 返回 "$change"作为一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36549129/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?