您好我正在使用ActiveAndroid将一些数据存储在本地数据库中,我得到一个非常奇怪的行为。我创建了我的表,当我尝试使用getId()检索元素的ID时,我得到一个空指针异常...但是所有其他字段都在那里而不是null。
之前有人遇到过这个吗?
以下是一些代码:
package ro.gebs.captoom.datamodel;
import java.io.Serializable;
import ro.gebs.captoom.database.CaptoomSQLiteHelper;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
@Table(name = CaptoomSQLiteHelper.TABLE_MILEAGE)
public class Mileage extends Model implements Serializable, Comparable<Mileage>{
private static final long serialVersionUID = 1L;
@Column(name = CaptoomSQLiteHelper.COLUMN_REMOTE_ID)
private String folderId;
@Column(name = CaptoomSQLiteHelper.COLUMN_START_DATE)
private String startDate;
@Column(name = CaptoomSQLiteHelper.COLUMN_END_DATE)
private String endDate;
@Column(name = CaptoomSQLiteHelper.COLUMN_DISTANCE)
private String distance;
@Column(name = CaptoomSQLiteHelper.COLUMN_CURRENCY)
private String currency;
@Column(name = CaptoomSQLiteHelper.COLUMN_UNIT)
private String unit;
@Column(name = CaptoomSQLiteHelper.COLUMN_PRICE)
private String price;
@Column(name = CaptoomSQLiteHelper.COLUMN_DETAILS)
private String details;
@Column(name = CaptoomSQLiteHelper.COLUMN_SYNC)
private int isSynchronized;
public Mileage(){
super();
}
public Mileage(String folder_id, String start_date, String end_date, String distance, String currency, String unit, String price, String details, int synced){
super();
this.folderId = folder_id;
this.startDate = start_date;
this.endDate = end_date;
this.distance = distance;
this.currency = currency;
this.unit = unit;
this.price = price;
this.details = details;
this.setIsSynchronized(synced);
}
@Override
public int compareTo(Mileage another) {
return this.getId().compareTo(another.getId());
}
public String getFolderId() {
return folderId;
}
public void setFolderId(String folderId) {
this.folderId = folderId;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getIsSynchronized() {
return isSynchronized;
}
public void setIsSynchronized(int isSynchronized) {
this.isSynchronized = isSynchronized;
}
@Override
public String toString(){
return this.details + "+++" + this.isSynchronized;
}
}
这是我尝试检索的地方:
package ro.gebs.captoom.database;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import ro.gebs.captoom.datamodel.Mileage;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.activeandroid.query.Select;
public class MileageDataSource {
// sync variables
public final int SYNC_ID = 0x7f020001;
public final int NEW_NOT_SYNC_ID = 0x7f020011;
// Database fields
private SQLiteDatabase database;
private CaptoomSQLiteHelper dbHelper;
private String[] columnsMileage = { CaptoomSQLiteHelper.COLUMN_ID,
CaptoomSQLiteHelper.COLUMN_START_DATE,
CaptoomSQLiteHelper.COLUMN_END_DATE,
CaptoomSQLiteHelper.COLUMN_DISTANCE,
CaptoomSQLiteHelper.COLUMN_CURRENCY,
CaptoomSQLiteHelper.COLUMN_UNIT, CaptoomSQLiteHelper.COLUMN_PRICE,
CaptoomSQLiteHelper.COLUMN_DETAILS,
CaptoomSQLiteHelper.COLUMN_SYNC,
CaptoomSQLiteHelper.COLUMN_REMOTE_ID };
private boolean isOpen;
public MileageDataSource(Context context) {
setDbHelper(new CaptoomSQLiteHelper(context));
isOpen = false;
}
public void open() {
if (!isOpen) {
isOpen = true;
}
}
public void close() {
if (isOpen) {
isOpen = false;
}
}
public Mileage createMileage(String folder_id, String start_date,
String end_date, String distance, String currency, String unit,
String price, String details) {
Mileage newMileage = new Mileage(folder_id, start_date, end_date,
distance, currency, unit, price, details, NEW_NOT_SYNC_ID);
newMileage.save();
return newMileage;
}
public Mileage updateMileage(long id, String folder_id, String start_date,
String end_date, String distance, String currency, String unit,
String price, String details) {
Mileage r = this.getMileageByID(id);
System.out.println("This mileage ID is the problem!!" + r.getId());
r.setFolderId(folder_id);
r.setStartDate(start_date);
r.setEndDate(end_date);
r.setDistance(distance);
r.setCurrency(currency);
r.setUnit(unit);
r.setPrice(price);
r.setDetails(details);
r.setIsSynchronized(NEW_NOT_SYNC_ID);
r.save();
return r;
}
public void syncMileage(Mileage r) {
r.setIsSynchronized(SYNC_ID);
r.save();
}
public Mileage getMileageByID(long id) {
Mileage r = new Mileage();
ArrayList<Mileage> allMileages = this.selectAllMileages();
for (int i = 0; i < allMileages.size(); i++) {
System.out.println("in getmileageby id:"
+ allMileages.get(i).getId());
if (allMileages.get(i).getId() == id) {
r = allMileages.get(i);
}
}
return r;
}
public ArrayList<Mileage> selectAllMileages() {
ArrayList<Mileage> allReceipts = new ArrayList<Mileage>();
List<Mileage> selMileages = new Select().from(
ro.gebs.captoom.datamodel.Mileage.class).execute();
allReceipts.addAll(selMileages);
return allReceipts;
}
public ArrayList<Mileage> findMileagessByDate(String myDate, long folder_id) {
ArrayList<Mileage> allMileages = new ArrayList<Mileage>();
List<Mileage> selMileages = new Select()
.from(ro.gebs.captoom.datamodel.Mileage.class)
.where(CaptoomSQLiteHelper.COLUMN_END_DATE + "='" + myDate
+ "' AND " + CaptoomSQLiteHelper.COLUMN_REMOTE_ID
+ " = " + folder_id).execute();
allMileages.addAll(selMileages);
return allMileages;
}
public ArrayList<Mileage> selectMileagesFromFolder(long folder_id) {
ArrayList<Mileage> allMiles = new ArrayList<Mileage>();
List<Mileage> selMiles = new Select()
.from(ro.gebs.captoom.datamodel.Mileage.class)
.where(CaptoomSQLiteHelper.COLUMN_REMOTE_ID + "=="
+ String.valueOf(folder_id)).execute();
allMiles.addAll(selMiles);
return allMiles;
}
public int getUnsyncedMileagesCount(long folder_id) {
List<Mileage> selReceipts = new Select()
.from(ro.gebs.captoom.datamodel.Mileage.class)
.where(CaptoomSQLiteHelper.COLUMN_REMOTE_ID + "=="
+ String.valueOf(folder_id) + " AND "
+ CaptoomSQLiteHelper.COLUMN_SYNC + "=="
+ String.valueOf(NEW_NOT_SYNC_ID)).execute();
return selReceipts.size();
}
public ArrayList<Mileage> selectUnsyncedMileages() {
ArrayList<Mileage> allMileages = new ArrayList<Mileage>();
List<Mileage> selReceipts = new Select()
.from(ro.gebs.captoom.datamodel.Mileage.class)
.where(CaptoomSQLiteHelper.COLUMN_SYNC + "=="
+ String.valueOf(NEW_NOT_SYNC_ID)).execute();
allMileages.addAll(selReceipts);
return allMileages;
}
public String[] getColumnsFolder() {
return columnsMileage;
}
public void setColumnsFolder(String[] columnsFolder) {
this.columnsMileage = columnsFolder;
}
public CaptoomSQLiteHelper getDbHelper() {
return dbHelper;
}
public void setDbHelper(CaptoomSQLiteHelper dbHelper) {
this.dbHelper = dbHelper;
}
public SQLiteDatabase getDatabase() {
return database;
}
public void setDatabase(SQLiteDatabase database) {
this.database = database;
}
}
此System.out.println("This mileage ID is the problem!!" + r.getId());
的输出为空....
答案 0 :(得分:0)
这可能不是答案,但我需要展示一段代码:
通过阅读AndroidActive(我从未使用过它),似乎你必须传递你想在newSelect()上选择的列。如果你想得到所有,它可能是这样的:
public ArrayList<Mileage> selectAllMileages() {
ArrayList<Mileage> allReceipts = new ArrayList<Mileage>();
List<Mileage> selMileages = new Select().all().from( //put all() between
ro.gebs.captoom.datamodel.Mileage.class).execute();
allReceipts.addAll(selMileages);
return allReceipts;
}
这可能是你的getId()返回null的原因。但是,就像我说的那样,这只是一个假设,我暂时无法测试它。