gpt4 book ai didi

android - 无法在后台线程上调用观察

转载 作者:行者123 更新时间:2023-12-03 13:57:37 26 4
gpt4 key购买 nike

我刚升级到AndroidX在完成我的所有要求之后。我让后台线程不运行但出现此错误。

private void getContactsList() {
Task.callInBackground((Callable<Void>) () -> {
mainActivityViewModel.geContacts(contactListRequest).observe(MainActivity.this, new Observer<ContactListResponse>() {
@Override
public void onChanged(@Nullable ContactListResponse contactListResponse) {
if(contactListRequest != null){
System.out.println("Successful");
}
}
});
return null;
}).continueWith((Continuation<Void, Void>) task -> {
if (task.isFaulted()) {
Log.e(TAG, "find failed", task.getError());
}
return null;
});
}



java.lang.IllegalStateException: Cannot invoke observe on a background thread
at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:443)
at androidx.lifecycle.LiveData.observe(LiveData.java:171)
at com.qucoon.rubies.MainActivity.lambda$getContactsList$12(MainActivity.java:887)
at com.qucoon.rubies.-$$Lambda$MainActivity$qPAxeGqyWT-wT3M7e8stM1rX2gQ.call(lambda)
at bolts.Task$4.run(Task.java:357)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

最佳答案

您需要在主线程中执行您的代码尝试调用 getContactsList()通过处理程序:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
getContactsList()
}
});

关于android - 无法在后台线程上调用观察,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54701090/

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