赞
踩
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
for (int i=0;i<10;i++){
subscriber.onNext(i);
}
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())//这里原来是要切换到主线程运行的
.subscribe(new Observer<Integer>() {
@Override
public void onCompleted() {
Log.i(MainActivity.class.getSimpleName(),"onCompleted");
}
@Override
public void onError(Throwable e) {
Log.i(MainActivity.class.getSimpleName(),"onError");
}
@Override
public void onNext(Integer i) {
Log.i(MainActivity.class.getSimpleName(),i+"");
}
});
}
});
上面这段代码一直报,只有主线程才能更新View
Only the original thread that created a view hierarchy can touch its views.
可能 有人发现了错误,细节啊
.subscribeOn(AndroidSchedulers.mainThread())
上面的这段代码“subscribeOn()”这个方法是设定Observable所在线程,所以,是将Observable切换到主线程了
正确的应该是调用”observeOn()”方法
.observeOn(AndroidSchedulers.mainThread())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。