gpt4 book ai didi

java - 如何在订阅开始时显示对话框?

转载 作者:行者123 更新时间:2023-12-01 16:52:33 25 4
gpt4 key购买 nike

我想在执行 Observable 时显示 ProgressBar:

Observable<T> observable;
Observer<T> observer;
...................
observable.doOnSubscribe(()->{showProgressBar();}
.finallyDo(()-> {hideProgressBar();})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);

...................
protected void showProgressBar() {
if (mProgressBar != null)
mProgressBar.setVisibility(View.VISIBLE);
}
protected void hideProgressBar() {
if (mProgressBar != null)
mProgressBar.setVisibility(View.GONE);
}

;

但我收到此错误:

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

在线mProgressBar.setVisibility(View.VISIBLE);

如何从 doOnSubscribe() 运行 showProgressBar()

最佳答案

observable.subscribeOn(Schedulers.newThread())
.doOnSubscribe(()->{showProgressBar();}
.subscribeOn(AndroidSchedulers.mainThread())
.finallyDo(()-> {hideProgressBar();})
.observeOn(AndroidSchedulers.mainThread())

应该可以工作。副作用运算符在调用它的任何线程上同步执行,因此只需向后工作即可。请参阅this gistthis discussion .

关于java - 如何在订阅开始时显示对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37070418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com