我从db的特定行获取所有记录,如下所示,
public StatutoryModel getStatutoryRow(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_KNOWLEDGE_STATUTORY, new String[] { KEY_ID,
KEY_STATUTORY_MAPPING, KEY_LOCATION, KEY_DESCRIPTION,KEY_ACTIVE,KEY_PROVISION,KEY_COMPLIANCE_TASK,KEY_FORMAT_FILE_LIST,KEY_DURATION,KEY_DOC_NAME,KEY_PENAL_CONSEQUENCE,KEY_DURATION_ID,KEY_FREQUENCY_ID,KEY_REPEAT_FREQ,KEY_COMPLIANCE_ID,KEY_REPEAT_TYPE_ID,KEY_STATUTORY_MONTH}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
StatutoryModel contact = new StatutoryModel(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), Integer.parseInt(cursor.getString(10)), cursor.getString(11), cursor.getString(12), Integer.parseInt(cursor.getString(13)), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18), cursor.getString(19));
// return contact
return contact;
}
我调用它如下,
Db.getInstance(getApplicationContext()).getStatutoryRow(1).getDescription()
问题是,当我尝试这样做时,我会获得numberformatexception。以下是我的错误日志
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.activities/com.activities.StatutoryDecision}:
java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:359)
at java.lang.Integer.parseInt(Integer.java:332)
at com.Controllers.Db.getStatutoryRow(Db.java:292)
at com.activities.StatutoryDecision.onCreate(StatutoryDecision.java:44)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
以下是我的模型类
public class StatutoryModel {
private int id;
private String description;
private String isStatutoryActive;
private String statutoryProvision;
private String complianceTask;
private String format_fileList;
private String duration;
private String document_name;
private String penalConsequences;
private String durationType_id;
private int frequencyId;
private String repeatsEvery;
private int complianceId;
private String repeatsTypeId;
String statutory_month;
private String statutory_date;
private String trigger_before_days;
private String repeat_by;
private String statuoryMapping;
private String location;
private static StatutoryModel ourInstance = new StatutoryModel();
public static StatutoryModel getInstance() {
return ourInstance;
}
public StatutoryModel() {
}
public StatutoryModel(int id, String description, String isStatutoryActive, String statutoryProvision, String complianceTask, String format_fileList, String duration, String penalConsequences, String document_name, String durationType_id, int frequencyId, String repeatsEvery, String repeatsTypeId, int complianceId, String statutory_month, String statutory_date, String trigger_before_days, String repeat_by, String statuoryMapping, String location) {
this.id = id;
this.description = description;
this.isStatutoryActive = isStatutoryActive;
this.statutoryProvision = statutoryProvision;
this.complianceTask = complianceTask;
this.format_fileList = format_fileList;
this.duration = duration;
this.penalConsequences = penalConsequences;
this.document_name = document_name;
this.durationType_id = durationType_id;
this.frequencyId = frequencyId;
this.repeatsEvery = repeatsEvery;
this.repeatsTypeId = repeatsTypeId;
this.complianceId = complianceId;
this.statutory_month = statutory_month;
this.statutory_date = statutory_date;
this.trigger_before_days = trigger_before_days;
this.repeat_by = repeat_by;
this.statuoryMapping = statuoryMapping;
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIsStatutoryActive() {
return isStatutoryActive;
}
public void setIsStatutoryActive(String isStatutoryActive) {
this.isStatutoryActive = isStatutoryActive;
}
public String getComplianceTask() {
return complianceTask;
}
public void setComplianceTask(String complianceTask) {
this.complianceTask = complianceTask;
}
public String getStatutoryProvision() {
return statutoryProvision;
}
public void setStatutoryProvision(String statutoryProvision) {
this.statutoryProvision = statutoryProvision;
}
public String getFormat_fileList() {
return format_fileList;
}
public void setFormat_fileList(String format_fileList) {
this.format_fileList = format_fileList;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getDocument_name() {
return document_name;
}
public void setDocument_name(String document_name) {
this.document_name = document_name;
}
public String getPenalConsequences() {
return penalConsequences;
}
public void setPenalConsequences(String penalConsequences) {
this.penalConsequences = penalConsequences;
}
public String getDurationType_id() {
return durationType_id;
}
public void setDurationType_id(String durationType_id) {
this.durationType_id = durationType_id;
}
public int getFrequencyId() {
return frequencyId;
}
public void setFrequencyId(int frequencyId) {
this.frequencyId = frequencyId;
}
public String getRepeatsEvery() {
return repeatsEvery;
}
public void setRepeatsEvery(String repeatsEvery) {
this.repeatsEvery = repeatsEvery;
}
public int getComplianceId() {
return complianceId;
}
public void setComplianceId(int complianceId) {
this.complianceId = complianceId;
}
public String getRepeatsTypeId() {
return repeatsTypeId;
}
public void setRepeatsTypeId(String repeatsTypeId) {
this.repeatsTypeId = repeatsTypeId;
}
public String getStatutory_month() {
return statutory_month;
}
public void setStatutory_month(String statutory_month) {
this.statutory_month = statutory_month;
}
public String getStatutory_date() {
return statutory_date;
}
public void setStatutory_date(String statutory_date) {
this.statutory_date = statutory_date;
}
public String getRepeat_by() {
return repeat_by;
}
public void setRepeat_by(String repeat_by) {
this.repeat_by = repeat_by;
}
public String getTrigger_before_days() {
return trigger_before_days;
}
public void setTrigger_before_days(String trigger_before_days) {
this.trigger_before_days = trigger_before_days;
}
public String getStatuoryMapping() {
return statuoryMapping;
}
public void setStatuoryMapping(String statuoryMapping) {
this.statuoryMapping = statuoryMapping;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
答案 0 :(得分:2)
int不能采取""或空值。 ""你想要一个int是一个字符串。因此,数字格式错误。而不是int使用Integer。使用该方法时,不要传递"",而是传递null。
答案 1 :(得分:2)
Integer.parseInt(cursor.getString(0))
这是在调用异常。所以cursor.getString(0)
没有返回一个数字,可能是因为它没有存储在你的数据库中。
答案 2 :(得分:2)
我认为,您的数据库具有KEY_ID的数字数据类型。那么你可以使用cursor.getInt(0)代替Integer.parseInt(cursor.getString(0))
替换此行。
StatutoryModel contact = new StatutoryModel(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), Integer.parseInt(cursor.getString(10)), cursor.getString(11), cursor.getString(12), Integer.parseInt(cursor.getString(13)), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18), cursor.getString(19));
到
StatutoryModel contact = new StatutoryModel(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getInt(10), cursor.getString(11), cursor.getString(12), cursor.getInt(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18), cursor.getString(19));
我希望这会对你有所帮助。感谢
答案 3 :(得分:2)
Try this
Before you convert into integer first check it's not empty or not blank
**Integer.parseInt(cursor.getString(0))**
Above statement when the cursor.getString(0) return int then no
issue but when return any string then give the number NumberFormatException.
so give proper validation before convert,
答案 4 :(得分:1)
String.valueOf()不会对待""作为一个数字,这是您获得此异常的主要原因。你应该传递0。
我想知道你如何调用这个方法getStatutoryRow()。我认为方法调用容易出错。