只有创建视图层次结构的原始线程才能触及它的视图。 Rx安卓

Only the original thread that created a view hierarchy can touch its views. RxAndroid

我尝试使用 RxAndroid

button.setOnClickListener(view -> restApiFactory.testService().getListTest(7)
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<Test>>() {

但我收到以下错误:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我没用过AndroidSchedulers.mainThread()

你需要从主线程操作 UI 所以为了做到这一点你需要告诉 rxandroid 通知主线程的变化所以使用

button.setOnClickListener(view -> restApiFactory.testService().getListTest(7)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //              ^^^^^^^^^^
                .subscribe(new Observer<List<Test>>() {

要获得它,你需要有一个依赖关系,因为

compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 

并且您当前的依赖项用于进行改造重新调整 rxAndroid 类型响应。

你应该使用 observeOn(AndroidSchedulers.mainThread()) 不是 subscribeOn(AndroidSchedulers.mainThread())

用作:-

.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())