我刚刚升级到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)
答案 0 :(得分:1)
您需要在主线程中执行代码,尝试通过处理程序调用{{1}}:
plugins {
id 'org.kordamp.gradle.jacoco' version '0.29.0'
}
config {
jacoco {
enabled
mergeExecFile
mergeReportHtmlFile
mergeReportXmlFile
additionalSourceDirs
additionalClassDirs
}
}
答案 1 :(得分:0)
如果对于Kotlin和Coroutines(使用AndroidX生命周期库2.2.0),则可以执行以下操作:
lifecycleScope.launch {
withContext(Dispatchers.Main) {
// Do something
}
}
如果不是lifecycleScope
和Lifecycle 2.2.0库,则可以定义自己的CoroutineScope并按原样使用它。
答案 2 :(得分:0)
使用@UiThreadTest注释测试方法
示例:
server.servlet.jsp.init-parameters.mappedfile=false
这对我有用