【问题标题】:Returning an Integer with RxJava - problem with subscribeOn使用 RxJava 返回整数 - subscribeOn 的问题
【发布时间】: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.Consumerio.reactivex.Observer。另外,删除this
  • 我把它改成了这样:.subscribe();但是 IDE 在两个 下都有一条红线,表示它需要一个括号或表达式?我不知道还要输入什么
  • 应该输入.subscribe(new Observer&lt;Integer&gt;() {。你熟悉 Java 语法吗?
  • 我把它改成了那个,它强迫我实现一个方法,它现在有自己的错误:.subscribe(new Observer() { @Override public void onSubscribe(Disposable d) {
  • 有 4 种方法,但我无法将它们全部粘贴:onSubscribe、onNext、Onerror 和 onComplete。错误是“无法解析方法订阅”匿名 io.reactivex.Observer)

标签: android mvvm rx-java android-livedata


【解决方案1】:

试试这个:

private void insert(Note note) {

    Log.d(TAG, "saveNote: called.");

    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
                }
            }, new io.reactivex.functions.Consumer<Throwable>() {
                @Override
                public void accept(Throwable error) {
                    // report the error
                }
            });
}

【讨论】:

  • 抛出一个空指针:( W/System.err: java.lang.NullPointerException: 尝试调用接口方法'io.reactivex.Single com.example.persistence.dao.NoteDao.insert(com .example.persistenc e.entity.Note)' 在空对象引用 W/System.err 上:在 com.example.ui.activities.NoteActivity.insert(NoteActivity.java:1 42) 在 com.example.ui.activities .NoteActivity.access$000(NoteActivity.java:43)
  • 您的noteDao 为空,因此必须弄清楚它应该初始化的原因和位置。
  • 我在类的顶部有这个:private NoteDao noteDao;它说它从未初始化。但是,如果我使用 // noteDao.insert(note) 将其注释掉,就会变成红色....这对我来说真的很困惑,当我在插入方法中使用它时,它怎么能不被初始化。
  • 您是编程新手还是来自 C 语言?您必须初始化对象,因为 Java 没有结构。你或许应该在Room 上做一个教程。
  • 是的,我是一名学生。我只是尝试插入并检索 id,但我观看的教程介绍了 MVVM、dagger、livedata 和一堆过于复杂的东西,这些东西刚刚让我的生活感到困惑,现在我迷路了。我只想要一种简单的插入方式并从活动中获取 id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
相关资源
最近更新 更多