gpt4 book ai didi

android - RxView.clicks 崩溃应用程序

转载 作者:行者123 更新时间:2023-11-29 14:13:00 27 4
gpt4 key购买 nike

尝试做一些基本的 RxJava 的东西来了解它是如何工作的,并试图在登录表单(使用 RxBindings 库)中显示按钮点击的进度,然后让它在一秒钟后消失。但是在显示进度并且我想隐藏它之后应用程序崩溃了。这是我尝试使用的代码,

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);

RxView.clicks(findViewById(R.id.email_sign_in_button))
.doOnNext(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
showProgress(true);
}
})
.observeOn(AndroidSchedulers.mainThread())
.delay(1, TimeUnit.SECONDS)
.subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
showProgress(false);
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
Toast.makeText(LoginActivity.this, "Threw: " + throwable, Toast.LENGTH_LONG).show();
}
});
}

private void showProgress(final boolean show) {
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
mLoginFormView.animate().setDuration(shortAnimTime).alpha(
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
}
});

mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
mProgressView.animate().setDuration(shortAnimTime).alpha(
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}
});
}

我得到的崩溃是,

ComposedException 1 : android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357) at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:909) at android.view.ViewGroup.invalidateChild(ViewGroup.java:4690) at android.view.View.invalidateInternal(View.java:11801) at android.view.View.invalidate(View.java:11765) at android.view.View.setFlags(View.java:9704) at android.view.View.setVisibility(View.java:6693) at android.widget.FrameLayout.setVisibility(FrameLayout.java:208) at com.sample.android.rxlogin.ui.LoginActivity.showProgress(LoginActivity.java:60) at com.sample.android.rxlogin.ui.LoginActivity.access$000(LoginActivity.java:19) at com.sample.android.rxlogin.ui.LoginActivity$1.accept(LoginActivity.java:44) at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:59) at io.reactivex.observers.SerializedObserver.onNext(SerializedObserver.java:111) at io.reactivex.internal.operators.observable.ObservableDelay$DelayObserver$1.run(ObservableDelay.java:84) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:200) at android.os.Handler.(Handler.java:114) at android.widget.Toast$TN.(Toast.java:344) at android.widget.Toast.(Toast.java:100) at android.widget.Toast.makeText(Toast.java:258) at com.sample.android.rxlogin.ui.LoginActivity$2.accept(LoginActivity.java:49) at com.sample.android.rxlogin.ui.LoginActivity$2.accept(LoginActivity.java:46) at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:72) at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:62) at io.reactivex.observers.SerializedObserver.onNext(SerializedObserver.java:111) at io.reactivex.internal.operators.observable.ObservableDelay$DelayObserver$1.run(ObservableDelay.java:84) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59) at io.reactivex.internal.schedulers.Schedul

我不明白为什么这会崩溃,因为我在主 Android 线程中使用 observeOn()。有人可以解释这里出了什么问题吗?正确的方法是什么?

最佳答案

只需将 .observeOn(AndroidSchedulers.mainThread()) 移动到 .delay(1, TimeUnit.SECONDS) block 之后。

有些运算符默认不对任何特定的调度器进行操作,但 delay 可以,正如您可以在 documentation 中看到的那样它在 computation() 调度程序中运行(至少是您正在使用的延迟版本)。

关于android - RxView.clicks 崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45130381/

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