这是我提醒的模型类
public class Remind extends RealmObject {
@Required
private String descripcion;
@Required
private String fecha;
@Required
private String hora;
@Required
private String titulo;
@PrimaryKey
private String id;
public Remind() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getHora() {
return hora;
}
public void setHora(String hora) {
this.hora = hora;
}
}
这是我获取上下文的配置类'因为我不知道是什么 这个课的问题:(
public class RemindMe extends Application {
@RealmModule(classes = {Remind.class})
public class SimpleRealmModule {}
private static RemindMe instance;
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration config = new RealmConfiguration.Builder(this).name("RemindMe.DB").build();
Realm.setDefaultConfiguration(config);
}
这是mi DAO的一部分,用于添加一些提醒
//这是一个有问题的类'导致Realm对象找不到 数据库上下文
public class RemindsDAO {
static boolean flag = false;
public boolean agregarRecordatorio(final Remind remind){
final Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Remind rem = realm.createObject(Remind.class);
rem.setId(UUID.randomUUID().toString());
rem.setTitulo(remind.getTitulo());
rem.setDescripcion(remind.getDescripcion());
rem.setHora(remind.getHora());
rem.setFecha(remind.getFecha());
}
}, new Realm.Transaction.Callback() {
@Override
public void onSuccess() {
realm.commitTransaction();
RemindsDAO.flag = true;
}
@Override
public void onError(Exception e) {
flag = false;
}
});
return flag;
}
}
答案 0 :(得分:4)
您需要在Manifest中注册您的Application类:
<application
android:name=".RemindMe">
your activities go here...
</application>
完成后,您应该能够在整个应用程序中使用Realm.getDefaultInstance(),而不会有任何麻烦。