【发布时间】:2019-11-23 03:34:26
【问题描述】:
@NonNull
private final NoteDao noteDao;
@Inject
public NoteRepository(@NonNull NoteDao noteDao) {
this.noteDao = noteDao;
}
试图完成这个从 Sqlite 数据库返回一个 int PK 的方法。
private void insert(Note note) {
Log.d(TAG, "saveNote: called.");
try {
noteDao.insert(note)
. map(new Function<Long, Integer>() {
@Override
public Integer apply(Long aLong) throws Exception {
long l = aLong;
return (int)l;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((new io.reactivex.functions.Consumer<Integer>() {
@Override
public void accept(Integer integerResource) {
// work with integerResource
sqCbtId = integerResource;
}
})); new io.reactivex.functions.Consumer<Throwable>() {
@Override
public void accept(Throwable error) {
// report the error
}
};
} catch (Exception e) {
e.printStackTrace();
}
* : java.lang.InstantiationException: java.lang.Class 没有零参数构造函数*
【问题讨论】:
-
您正在使用
androidx.lifecycle.Observer。请改用io.reactivex.functions.Consumer或io.reactivex.Observer。另外,删除this。 -
我把它改成了这样:.subscribe(
);但是 IDE 在两个 下都有一条红线,表示它需要一个括号或表达式?我不知道还要输入什么 -
应该输入
.subscribe(new Observer<Integer>() {。你熟悉 Java 语法吗? -
我把它改成了那个,它强迫我实现一个方法,它现在有自己的错误:.subscribe(new Observer
() { @Override public void onSubscribe(Disposable d) { -
有 4 种方法,但我无法将它们全部粘贴:onSubscribe、onNext、Onerror 和 onComplete。错误是“无法解析方法订阅”匿名 io.reactivex.Observer)
标签: android mvvm rx-java android-livedata